Check open form

  • Thread starter Thread starter George Varelas
  • Start date Start date
G

George Varelas

I have a start up form in my windows application which is an mdi form. Can
anyone tell me how can I check if an mdi child form in my application is
already open so I won't load a new instance of the same form but just give
the already opened form focus?

Thank you
George
 
George Varelas said:
I have a start up form in my windows application which is an mdi form. Can
anyone tell me how can I check if an mdi child form in my application is
already open so I won't load a new instance of the same form but just give
the already opened form focus?

\\\
Private m_Child As Form2

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
If m_Child Is Nothing Then
m_Child = New Form2()
m_Child .MdiParent = Me
AddHandler m_Child.Load, AddressOf Me.MdiChild_Load
AddHandler m_Child.Closed, AddressOf Me.MdiChild_Closed
m_Child.Show()
Else
m_Child.Activate()
End If
End Sub

Private Sub MdiChild_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox( _
"MDI child with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" loaded!" _
)
End Sub

Private Sub MdiChild_Closed( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox( _
"MDI child with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" closed!" _
)
m_Child = Nothing
End Sub
///
 
I forgot to mention that I use C#.


Herfried K. Wagner said:
\\\
Private m_Child As Form2

Private Sub Button1_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Button1.Click
If m_Child Is Nothing Then
m_Child = New Form2()
m_Child .MdiParent = Me
AddHandler m_Child.Load, AddressOf Me.MdiChild_Load
AddHandler m_Child.Closed, AddressOf Me.MdiChild_Closed
m_Child.Show()
Else
m_Child.Activate()
End If
End Sub

Private Sub MdiChild_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox( _
"MDI child with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" loaded!" _
)
End Sub

Private Sub MdiChild_Closed( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox( _
"MDI child with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" closed!" _
)
m_Child = Nothing
End Sub
///
 
George,

How about going through Form.MdiChildren collection and looking for your
form there?
 
George said:
I have a start up form in my windows application which is an mdi form. Can
anyone tell me how can I check if an mdi child form in my application is
already open so I won't load a new instance of the same form but just give
the already opened form focus?

Thank you
George

' frmName is string variable containing the name of the form in
question
if My.Application.OpenForms(frmName).IsHandleCreated = True Then
' form is open
else
' form is closed
endif
 
Back
Top