MDI Forms

  • Thread starter Thread starter Adriano
  • Start date Start date
A

Adriano

hello,

what is the property or event to check if the child form is alredy open? I
just want to avoid opening a child form inside the parent form twice.

thanks in advance
 
Adriano said:
what is the property or event to check if the child form is alredy
open? I just want to avoid opening a child form inside the parent
form twice.

Store the reference to the child in a variable (=field) in the MDI parent.
Add an event handler to the child's closed event, and...... here's the
code:


private m_Child As ChildForm
'...
sub bla
if m_child is nothing
m_child = new childform
addhandler m_child.closed, addressof onchildClosed
end if
m_child.show
m_child.activate
end sub

sub ochildclosed( _
ByVal sender As Object, ByVal e As System.EventArgs)

removehandler m_child.closed, addressof onchildClosed
m_child = nothing
end sub

- OR -

Loop over the MDI parent's Mdichildren property:

dim f as form
dim found as boolean

for each f in me.mdichildren
if typeof f is ChildForm then
found = true
exit for
end if
next

if not found then
f = new ChildForm
f.show
end if

f.activate


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
* "Adriano said:
what is the property or event to check if the child form is alredy open? I
just want to avoid opening a child form inside the parent form twice.

\\\
Private m_Child As Form2

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.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
End If
m_Child.Show() ' ...
End Sub

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

Private Sub MdiChild_Closed( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
)
MsgBox( _
"MdiChild with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" closed!" _
)
End Sub
///
 
Herfried,
One minor addition:
Private Sub MdiChild_Closed( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
)
MsgBox( _
"MdiChild with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" closed!" _
)


m_child = nothing

Which will allow a new Form2 to be created after the first Form2 is
closed... :-)


As an alternative I will implement Form2 as a Singleton itself. Something
like:

Public Form2
Inherits Form

' form designer created this function, we are just making it private
Private Sub New
end sub

private shared m_instance as Form2

public shared readonly property Instance As Form2
get
if m_instance is nothing then m_instance = new form2
return m_instance
end get
end property

private sub form2_closed(...) handles mybase.closed
m_instance = nothing
end sub

Which will reset the instance variable when the form is closed, allowing a
new instance to be created when needed...

Hope this helps
Jay
 
Thank you all,
all codes work great!

Adriano


Jay B. Harlow said:
Herfried,
One minor addition:
Private Sub MdiChild_Closed( _
ByVal sender As Object, _
ByVal e As System.EventArgs _
)
MsgBox( _
"MdiChild with handle " & _
DirectCast(sender, Form).Handle.ToString() & _
" closed!" _
)


m_child = nothing

Which will allow a new Form2 to be created after the first Form2 is
closed... :-)


As an alternative I will implement Form2 as a Singleton itself. Something
like:

Public Form2
Inherits Form

' form designer created this function, we are just making it private
Private Sub New
end sub

private shared m_instance as Form2

public shared readonly property Instance As Form2
get
if m_instance is nothing then m_instance = new form2
return m_instance
end get
end property

private sub form2_closed(...) handles mybase.closed
m_instance = nothing
end sub

Which will reset the instance variable when the form is closed, allowing a
new instance to be created when needed...

Hope this helps
Jay


open?
 
Just a stupid question probably, but is the AddHandler m_Child.Load,
AddressOf Me.MdiChild_Load really necessary?
It seems to me like you don't really need that line and the sub.
 
Back
Top