Check open form

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
 
H

Herfried K. Wagner [MVP]

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
///
 
G

George Varelas

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
///
 
S

Stoitcho Goutsev \(100\)

George,

How about going through Form.MdiChildren collection and looking for your
form there?
 
K

Kathy

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top