我想检测窗体是否打开,如果是,那么我想把它放在前面而不是再打开它。
我知道我需要一个表单集合,但我想知道是否有一个内置的表单集合,它包含VB.NET中的所有表单,或者我需要实现自己的表单。
谢谢。
答案 0 :(得分:1)
你可以尝试:
'Pass the form object (you could also use string as the
'parameter and replace the if condition to: "If form.Name.Equals(targetForm) Then")
Public Sub BringToFront(ByVal targetForm As Form)
Dim form As Form
For Each form In Application.OpenForms()
If form Is targetForm Then
form.BringToFront()
Exit For
End If
Next
End Sub
如果你需要在前面带一个特定的表格(只有它已经加载),请调用此子目录,如下所示:
BringToFront(targetformobject)
答案 1 :(得分:0)
使用LINQ:
Dim existingForm = Application.OpenForms.OfType(Of YourFormType)().FirstOrDefault()
答案 2 :(得分:0)
我总是P / Invoke代码调用user32 windows dll。这样的事情应该做你想做的事。
<DllImport("user32.dll")> _
Public Shared SetFocus(ByVal hwnd As IntPtr)
Private Sub SetFocusToExistingWindow()
Dim procs As Process() = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
If procs.Length > 0 Then
Dim hwnd As IntPtr = procs(0).MainWindowHandle
SetFocus(hwnd)
End If
End Sub