Noob Question: Allowing only one open form

panuvin

Member
Joined
Jul 18, 2005
Messages
5
Location
South Florida
Programming Experience
5-10
Hey all,

I'm in the process of learning VB.NET and have a hopefully simple question regarding forms. I have a main form and I have a button and a main menu link that allows a new form to open. However, if you keep clicking new, the new form will keep opening. I'd like only one form to be open at a time if that's possible. How can I do that? And if it's easy, how can I shade the button and file (in the main menu) if the new form is already open...showing the user they can't open another instance of the form? Thanks so much for your help!

Panuvin
 
Last edited:
I think I may have seen this question answered on another forum, but here goes anyway:
VB.NET:
	Private WithEvents childForm As Form2

	Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
		'Show the child form.
		Me.childForm = New Form2
		Me.childForm.Show()

		'Disable the menu and button.
		Me.MenuItem1.Enabled = False
		Me.Button1.Enabled = False
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Me.MenuItem1.PerformClick()
	End Sub

	Private Sub childForm_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles childForm.Closed
		'Enable the menu button.
		Me.MenuItem1.Enabled = True
		Me.Button1.Enabled = True
	End Sub
An alternative would be to leave the menu item and button enabled and have them activate the existing form, if one is open, rather than open a new one:
VB.NET:
    Private childForm As Form2

	Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
		If Me.childForm Is Nothing OrElse Me.childForm.IsDisposed Then
			'Show a new child form.
			Me.childForm = New Form2
			Me.childForm.Show()
		Else
			'Activate the existing child form.
			Me.childForm.Activate()
		End If
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Me.MenuItem1.PerformClick()
	End Sub
 
How is this different?

Hello,

First off, let me thank you for your advice. I got another reply that seemed similar and I was hoping you could decipher the differences between the two approaches. Here is the reply; please let me know what you think:

Hi panuvin,

What you want is to set then Enabled property of the Button and/or the menu button to false every time it is clicked. Then in the closing event of form2 put

frm1.ButtonNew.enabled =True

etc

For this to work, make sure the modifiers property of the button etc is set to Public or Friend. I am assuming that your form1 has been declared with the default status of Public.

You could also check the status of frm2 when the user tries to create the instance and disallow the operation if necessary, with something like

visual basic code:
If frm2 Is Nothing Then
MessageBox.Show("Frm2 is already in existence")
Exit Sub
End If
Dim frm2 As New fcls2
frm2.Show

then you won't need to disable the buttons.
 
That is similar to my first code example except that it is re-enabling the button on the parent form from the child. In order to do that, the child form would have to have a reference to the parent, which the code that person posted does not provide. Given that the parent already has a reference to the child I would just use that, as in my first example. My preference would be to use my second example, which does not disable the button at all but simply activates the existing form if the button is pressed again, but opens a new form if the previous one has been closed.
 
Back
Top