How to only open a form once

carlw

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

I need to be able to open a form from a button click but once open i need to stop the button opening another instance of that form until the first i closed.
This is what i have so far but it doesnt allow the form to open once.

VB.NET:
 [size=2][color=#0000ff]
Private Sub [/color][/size][size=2]MapBClick(sender [/size][size=2][color=#0000ff]As [/color][/size][size=2]System.[/size][size=2][color=#0000ff]Object[/color][/size][size=2], e [/size][size=2][color=#0000ff]As [/color][/size][size=2]System.EventArgs)
[/size][size=2][color=#0000ff]Dim [/color][/size][size=2]iyardref [/size][size=2][color=#0000ff]As Integer [/color][/size][size=2]=f.intMapB
[/size][size=2][color=#0000ff]Dim [/color][/size][size=2]fFormMapDraw [/size][size=2][color=#0000ff]As new [/color][/size][size=2]mapdraw(iyardref)
	[/size][size=2][color=#0000ff]If Not [/color][/size][size=2]IsNothing(fFormMapDraw) [/size][size=2][color=#0000ff]Then 
	 If Not [/color][/size][size=2]fFormMapDraw.IsDisposed [/size][size=2][color=#0000ff]Then
	 [/color][/size][size=2]fFormMapDraw.WindowState = FormWindowState.[/size][size=2][color=#0000ff]Normal [/color][/size][size=2][color=#008000]	 [/color][/size][size=2]			 [/size]
[size=2]	 fFormMapDraw.BringToFront()[/size]
[size=2][color=#0000ff]Else
	 [/color][/size][size=2]fFormMapDraw = [/size][size=2][color=#0000ff]New [/color][/size][size=2]mapdraw(iyardref)
	 fFormMapDraw.Show()
	 fFormMapDraw.MdiParent = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].MdiParent
[/size][size=2][color=#0000ff]End If
Else
	[/color][/size][size=2]fFormMapDraw = [/size][size=2][color=#0000ff]New [/color][/size][size=2]mapdraw(iyardref)
	fFormMapDraw.MdiParent = [/size][size=2][color=#0000ff]Me[/color][/size][size=2].MdiParent
	fFormMapDraw.Show()
[/size][size=2][color=#0000ff]End If
 
End Sub
[/color][/size]
If anyone could help me with where im going wrong i would really appreciate it.

Thanks

Carl
 

malitice

Active member
Joined
Feb 20, 2005
Messages
41
Programming Experience
1-3
just use variables to see if it is allowed to open it.

eg

public Shared noopen as integer

if noopen = 1 then

elseif noopen = 0 then
form2.show
noopen = 1
end if

Then in form2 have an on closing event that sets form1.noopen back to 0.
 

khan_zahid

Member
Joined
Feb 23, 2005
Messages
10
Programming Experience
1-3
Here is the way I was able to stop opening child window more then once.


Create a Function:
Private Function CheckIfOpen(ByVal frmName As String) As Boolean
Dim frm As Form
For Each frm In Me.MdiChildren
If frm.Name = frmName Then
frm.Focus()
Return true
Exit Function
End If
Next
Return False
End Function

add code to where you open the form. (menu or button)

If CheckIfOpen("frmVoidPrintedCoupons")=False Then
Dim fv As New frmVoidPrintedCoupons
fv.MdiParent = Me
fv.Show()
End If

Pass the name of the child window to the function. Loop through each mdichild. If found then set forcus to the one that is open else return true and open the window for the first time.


There might be a better way to do this, If so please let me know.
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The way khan_zahid has shown is a vaible solution, especially if dealing with multiple forms.
Another way to accomplish this is to create a class level variable for the form (by declaring it outside of any procedure).
Then you check to see if it's nothing or has been disposed, if so create a new one.
Your code:
Dim fFormMapDraw As new mapdraw(iyardref) will always create a new instance.
 
Top Bottom