繁体   English   中英

如何在打开对话框的窗体窗口顶部打开 VB.Net 对话框窗口?

[英]How to open VB.Net dialog window on top of form window that is opening the dialog?

在我的 Form1 类中,我有一个名为 beginProcessingItems() 的方法,它对项目列表进行操作。 这个列表可能非常大,所以我在一个新线程中执行 beginProcessingItems 方法,如下所示:

Dim processThread As New Thread(AddressOf beginProcessingItems)
processThread.Start()

有时我需要显示一个对话框来从用户那里收集有关某个项目的附加信息。 该对话框是在 beginProcessingItems() 方法中创建和打开的,该方法现在在与我的 Form1 窗口不同的线程中运行。

当我打开对话框时,它在 Form1 窗口后面加载。 我在其他堆栈帖子中尝试了各种建议,但它们最终都导致跨线程操作无效异常。

这是我目前必须打开对话框的代码:

Public Sub beginProcessingItems()  
    ' ..do stuff .. and sometimes:
    Dim IDD As New ItemDetailsDialog()
    IDD.Location = ImportItemsButton.Location ' sets X,Y coords
    IDD.StartPosition = FormStartPosition.Manual
    IDD.TopMost = True
    'Note: Me = The Form1 object
    'IDD.Parent = Me '<-- also throws exception.
    If IDD.ShowDialog(Me) = DialogResult.OK Then ' <-- If I remove "Me" then the dialog opens but its underneath the Form1 window.
       ' .. do stuff with the dialog results
    End If
End Sub

这是异常消息:

跨线程操作无效:从创建它的线程以外的线程访问控件“Form1”。

我能够将代码编入使用委托工作的东西中。

在 Form1 类中,我添加了:

' = Created a public property to hold the dialog object ======
Public IDD As ItemDetailDialog = Nothing

' = Created a Delegate ======
Delegate Sub DelegateShowItemDetailDialog(ByVal Param1 As Integer, ByRef SomeClassObj As SomeClass, ByVal Param3 As Integer)

' = Created a method that instantiates the IDD  property and shows the dialog =====
Private Sub ShowItemDetailDialog(ByVal Param1 As Integer, ByRef SomeClassObj As SomeClass, ByVal Param3 As Integer)
    IDD = New ItemDetailDialog(Param1, SomeClassObj, Param3)
    IDD.Location = ImportItemsButton.Location ' sets X,Y coords
    IDD.StartPosition = FormStartPosition.Manual
    'IDD.TopMost = True ' <-- not needed
    'IDD.Parent = Me ' <-- not needed
    IDD.ShowDialog()        
End Sub

' = Instantiate the Delegate object into a public property 
' = that will get invoked from the beginProcessingItems() method running in a separate thread
Public ThreadShowItemDetailDialog As New  DelegateShowItemDetailDialog(AddressOf ShowItemDetailDialog)

我修改了 beginProcessingItems 方法如下:

Public Sub beginProcessingItems()  
    ' ..do stuff .. and sometimes:
    Me.Invoke(ThreadShowItemDetailsDialog, {Param1, SomeClassObj, Param3})
    ' dialog is now on top of the form1 window :-)
    If IDD.DialogResult = DialogResult.OK Then
       ' .. do stuff with the dialog results via the IDD class object
    End If
End Sub

请注意,Param1、SomeClassObj 和 Param3 变量显示为如何将参数值传递给对话框对象的示例。 显然,您可以将它们更改为对话框对象实例化器需要的任何内容。

尝试将其放入您的 Sub Form_Load 中:

CheckForIllegalCrossThreadCalls = False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM