FormCollection in VB.NET

I want to determine if the window form is open, and if so, I would like to bring it in front, and not open it again.

I know that I need a collection of forms for this, but I want to know if there is a built-in collection of forms that contains all the forms in VB.NET or whether I need to implement my own.

Thank.

+3
source share
3 answers

You can try:

'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

Call this subsection if you need to bring a specific shape in front (only if it is already loaded) as follows:

BringToFront(targetformobject)
+1
source

Use property Application.OpenForms.

Using LINQ:

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

P/Invoke dll user32. - , .

    <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
0
source

Source: https://habr.com/ru/post/1736038/


All Articles