繁体   English   中英

VB.NET中的FormCollection

[英]FormCollection in VB.NET

我想检测窗体是否打开,如果是,那么我想把它放在前面而不是再打开它。

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

谢谢。

你可以尝试:

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

使用Application.OpenForms属性。

使用LINQ:

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

我总是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

暂无
暂无

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

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