调用表单 showdialog 不是模态的
Invoke form showdialog is not modal
我有 2 个表单,1 个 MainForm 和 1 个 Form2。我正在尝试将 Form2 显示为 MainForm 的模态窗体和背景。这是我目前所拥有的。
出现默认的 MainForm,5 秒后它将显示 Form2 作为来自后台线程的模态窗体。我关闭 Form2,如果使用 ShowDialog 再次显示相同的 Form2,则该表单不是模态的。如何确保显示的 Form2 始终是模态的?
Public Class MainForm
Dim frm2 As Form2
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
frm2 = New Form2()
Dim frmHandle As IntPtr = frm2.Handle
frm2.Button1.Text = "test"
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf DoSomething), 0)
End Sub
Private Sub DoSomething()
'call show dialog first time
Threading.Thread.Sleep(5000)
If frm2.InvokeRequired Then
frm2.Invoke(New Action(AddressOf frm2.ShowDialog))
Else
frm2.ShowDialog()
End If
'call show dialog second time
If frm2.InvokeRequired Then
frm2.Invoke(New Action(AddressOf frm2.ShowDialog))
Else
frm2.ShowDialog()
End If
End Sub
End Class
在 showDialog 中,您可以设置使子项变为模态的父窗体:
Public Class MainForm
Dim frm2 As Form2
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
frm2 = New Form2()
Dim frmHandle As IntPtr = frm2.Handle
frm2.Button1.Text = "test"
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf DoSomething), 0)
End Sub
Private Sub DoSomething()
Dim myAction as Action(Of System.Windows.Forms.IWin32Window)
'call show dialog first time
Threading.Thread.Sleep(5000)
If Me.InvokeRequired Then
myAction = AddressOf frm2.ShowDialog
Me.Invoke(myAction(Me))
Else
frm2.ShowDialog(Me)
End If
'call show dialog second time
If Me.InvokeRequired Then
myAction = AddressOf frm2.ShowDialog
Me.Invoke(myAction(Me))
Else
frm2.ShowDialog(Me)
End If
End Sub
End Class
您可以使用以下方法缩短代码:
New Action(Of System.Windows.Forms.IWin32Window)(AddressOf frm2.ShowDialog), Me)
我有 2 个表单,1 个 MainForm 和 1 个 Form2。我正在尝试将 Form2 显示为 MainForm 的模态窗体和背景。这是我目前所拥有的。
出现默认的 MainForm,5 秒后它将显示 Form2 作为来自后台线程的模态窗体。我关闭 Form2,如果使用 ShowDialog 再次显示相同的 Form2,则该表单不是模态的。如何确保显示的 Form2 始终是模态的?
Public Class MainForm
Dim frm2 As Form2
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
frm2 = New Form2()
Dim frmHandle As IntPtr = frm2.Handle
frm2.Button1.Text = "test"
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf DoSomething), 0)
End Sub
Private Sub DoSomething()
'call show dialog first time
Threading.Thread.Sleep(5000)
If frm2.InvokeRequired Then
frm2.Invoke(New Action(AddressOf frm2.ShowDialog))
Else
frm2.ShowDialog()
End If
'call show dialog second time
If frm2.InvokeRequired Then
frm2.Invoke(New Action(AddressOf frm2.ShowDialog))
Else
frm2.ShowDialog()
End If
End Sub
End Class
在 showDialog 中,您可以设置使子项变为模态的父窗体:
Public Class MainForm
Dim frm2 As Form2
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
frm2 = New Form2()
Dim frmHandle As IntPtr = frm2.Handle
frm2.Button1.Text = "test"
System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf DoSomething), 0)
End Sub
Private Sub DoSomething()
Dim myAction as Action(Of System.Windows.Forms.IWin32Window)
'call show dialog first time
Threading.Thread.Sleep(5000)
If Me.InvokeRequired Then
myAction = AddressOf frm2.ShowDialog
Me.Invoke(myAction(Me))
Else
frm2.ShowDialog(Me)
End If
'call show dialog second time
If Me.InvokeRequired Then
myAction = AddressOf frm2.ShowDialog
Me.Invoke(myAction(Me))
Else
frm2.ShowDialog(Me)
End If
End Sub
End Class
您可以使用以下方法缩短代码:
New Action(Of System.Windows.Forms.IWin32Window)(AddressOf frm2.ShowDialog), Me)