Is MDI child open or not?

J

JeremyGrand

I'm trying to detect if a given mdi child is open in my app using the
following code in the main form (if it is open, I want to view it, not
create a new instance):


Public Function isOpen(ByVal frmName As String) As Boolean
Dim i As Int16
isOpen = False
For i = 0 To Me.MdiChildren.Length - 1
If DirectCast(Me.MdiChildren(i), Form).Name = frmName Then
isOpen = True
End If
Next
end

The problem is, this seems to detect the existence of dead forms.

Is there a way to do what I want?
 
L

Lucky

what do you mean by "dead form". as far as i know, whenever you unload
or dispose the child form, mdi form leaves the reference of that form
so how come you are getting such references?
it shouldnt happen at all.

check out your code again

Lucky
 
H

Herfried K. Wagner [MVP]

JeremyGrand said:
I'm trying to detect if a given mdi child is open in my app using the
following code in the main form (if it is open, I want to view it, not
create a new instance):

\\\
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
///
 
J

JeremyGrand

Herfried, thanks for pointing me in the right direction. The FormClosed
event is exactly what I was looking for.

I prefer to put code related to a form in the form itself and I have a
custom session singleton, so my implementation is a little different than
your suggestion:

Public Class frmMyForm
Inherits System.Windows.Forms.Form
Shared frm As frmMyForm= Nothing
....
Public Shared Sub Execute(ByRef MySession as ..)
If frmMyForm.frm Is Nothing Then
frm = New frmMyForm
With frm
.MdiParent = MySession.MDIParent
....
end with
end if
frm.Show()
end sub
....
Private Sub frmMyForm_FormClosed(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
frm = Nothing
End Sub

Jeremy
 
M

Martin

I wrote these two small procedures to start a form. Works perfectly and
prevents double instances

' This is the procedure to call to open a form
Private Sub OpenView(ByVal FormName As String)
If My.Application.OpenForms(FormName) Is Nothing Then
ShowFormByName(FormName)
Else
My.Application.OpenForms(FormName).Activate()
End If
End Sub

' This is an underlaying sub, called from the one above
Private Sub ShowFormByName(ByVal FormName As String)
Dim ThisForm As Form
Dim ThisType As Type
ThisType = Type.GetType("appEspital." & FormName)
ThisForm = DirectCast(Activator.CreateInstance(ThisType), Form)
ThisForm.MdiParent = Me
ThisForm.Show()
End Sub
 

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