Home All Groups Group Topic Archive Search About

Is MDI child open or not?

Author
17 Jan 2006 2:17 AM
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?

Author
17 Jan 2006 4:49 AM
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
Author
17 Jan 2006 2:36 PM
Herfried K. Wagner [MVP]
"JeremyGrand" <jer***@ninprodata.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
17 Jan 2006 4:51 PM
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
Author
31 Jan 2006 11:26 AM
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




Show quoteHide quote
"JeremyGrand" <jer***@ninprodata.com> wrote in message
news:e4CKowwGGHA.3532@TK2MSFTNGP14.phx.gbl...
> 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?
>