2010-03-09 56 views
3

我想检测窗口窗体是否打开,如果是,那么我想把它放在前面而不是再次打开它。VB.NET中的FormCollection

我知道我需要一个表单集合,但我想知道是否有内置的表单集合,它包含了VB.NET中的所有表单,或者我需要实现自己的表单集合。

谢谢。

回答

0

使用Application.OpenForms属性。

使用LINQ:

Dim existingForm = Application.OpenForms.OfType(Of YourFormType)().FirstOrDefault() 
0

我一直的P/Invoke的代码来调用到USER32 Windows动态链接库。像这样的东西应该做你想要的。

<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 
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)