Showing a window not opening a new one

carlw

Member
Joined
Dec 30, 2004
Messages
18
Programming Experience
Beginner
Hi,

I have have a problem. I have 3 forms, Form 3 needs to be opened from a button click on form 2 and brought to the front from a menu click on form 1. The problem i have is that i cant get the 2 clicks to interact with each other. I have it so that each click will only allow one instant of the form to be open at any one time but that only allows one from that click it doesnt stop it being opened from the other.

Thanks

Carl W
 

carlw

Member
Joined
Dec 30, 2004
Messages
18
Programming Experience
Beginner
This is on form 1


Public Sub
MenuItem4click(sender As System.Object, e As System.EventArgs)
If Not IsNothing(F2) Then



If Not
F2.IsDisposed Then

' then it must already be instantiated - maybe it's

' minimized or hidden behind other forms ?

F2.WindowState = FormWindowState.Normal ' Optional

F2.BringToFront() ' Optional



Else

' else it has already been disposed, so you can

' instantiate a new form and show it

F2 = New Form3()

f2.MdiParent =
Me

F2.Show()

End If

Else

' else the form = nothing, so you can safely

' instantiate a new form and show it

F2 = New Form3()

f2.MdiParent =
Me

F2.Show()

End If

End Sub

 

carlw

Member
Joined
Dec 30, 2004
Messages
18
Programming Experience
Beginner
This is on form 2


Public Sub
Button1Click(sender As System.Object, e As System.EventArgs)
If Not IsNothing(F2) Then

' and if it hasn't been disposed yet

If Not F2.IsDisposed Then

' then it must already be instantiated - maybe it's

' minimized or hidden behind other forms ?

F2.WindowState = FormWindowState.Normal ' Optional

F2.BringToFront() ' Optional

Else

' else it has already been disposed, so you can

' instantiate a new form and show it

F2 = New Form3()

f2.MdiParent =
Me.MdiParent

F2.Show()

End If

Else

' else the form = nothing, so you can safely

' instantiate a new form and show it

F2 = New Form3()

f2.MdiParent =
Me.MdiParent

F2.Show()

End If

End Sub

 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
If F2 is declared in a module, the code should work.
But note that the above procedures are missing the Handles clause (Handles MenuItem4.Click, Handles Button1.Click).

You could also simplify the code to something like:
VB.NET:
If F2 Is Nothing OrElse F2.IsDisposed Then
    F2 = New Form3()
End If
F2.MdiParent = Me.MdiParent
F2.Show()
F2.WindowState = FormWindowState.Normal ' Optional
F2.BringToFront() ' Optional
Note that using [ code ] blocks aids in the reading and formatting of code.
The new advanced editor has a button (the pound sign #).
 
Last edited:
Top Bottom