Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if form is Opened

Tags:

vb.net

I give this question for more knowledge. How can I know if the form is Opened in my application or not, in order not to open it again I mean not to create an instance of the same form while it's running

   Dim frmCollection As New FormCollection()
    frmCollection = Application.OpenForms()
    If frmCollection.Item("Form2").IsHandleCreated Then
        MsgBox("Yes Opened")
    Else
        Dim f As New Form2()
        With f
            .Text = "form2"
            .Show()
        End With
    End If

if I executes this code many times it will create more instances of the form Form2 How can I check if this form is not already opened

like image 605
Mohammed Khaled Avatar asked Sep 06 '25 16:09

Mohammed Khaled


2 Answers

You can try it like this:

 Imports System.Linq ' need to add 


If Application.OpenForms().OfType(Of Form2).Any Then
  MessageBox.Show("Opened")
Else
  Dim f2 As New Form2
  f2.Text = "form2"
  f2.Show()
End If
like image 165
LarsTech Avatar answered Sep 11 '25 18:09

LarsTech


You can use the following code:

If myForm.IsHandleCreated then
   myForm is open
End If
like image 23
user3124064 Avatar answered Sep 11 '25 18:09

user3124064