Detecting if a form is open

Stoffel

Member
Joined
Dec 11, 2004
Messages
16
Location
Belgium
Programming Experience
1-3
Hi

I am making a program with a MDI form and a menu
In the menu you can open a form but with different options (one to add data, one to just view data)

How can I detect if the form is still open so it won't be opened for a second time?

Thanks
 
this checks to see if frmAbout is already open, if it is then it doesnt open a new one, if not then an instance is made and it's opened this code also resides in the MDI Parent form as well

VB.NET:
 Dim blnFound As Boolean = False
 		Dim frmTest As Form
 		'Does form already exist?
 		For Each frmTest In Me.MdiChildren
 			With frmTest
 				If .Name = "frmAbout" Then
 				    .Activate()   'Activate previous instance
 				    blnFound = True
 				End If
 			End With
 		Next
 		If blnFound = False Then
 			frmAbout = New frmAbout
 			With frmAbout
 				.MdiParent = Me
 				.Show()
 			End With
 		End If
 
VB.NET:
  'module level variables
  private frmMyForm as Form1
  
  'Makeing the form above a child
  
  frmMyForm = new Form1
  frmMyForm.MdiParent = me
  frmMyForm.Show()
  
  'Useing a textbox on that form
  frmMyForm.Textbox1.text = "This is from Parent Form"
 
JuggaloBrotha said:
VB.NET:
'module level variables
private frmMyForm as Form1
 
'Makeing the form above a child
 
frmMyForm = new Form1
frmMyForm.MdiParent = me
frmMyForm.Show()
 
'Useing a textbox on that form
frmMyForm.Textbox1.text = "This is from Parent Form"
Using that, it will open a new form,
but I would like to combine it with the code posted above.
Is that possible?

This is what it should do:
I use the same form for showing and adding items.
When it shows items, textboxes are filled in
But when I want to add items, I want to use the same form, but the textboxes have to be empty, readonly is false en a button to add the data should be visible too.
 
Solved the problem

I use this code:
VB.NET:
	Dim n As frmTest
 
	'Is the form open
	If Not (n Is Nothing OrElse n.IsDisposed) Then
	 With n
		 'Do stuff
		 .Activate()
	 End With
	Else
	 n = New frmAbout
	 With n
		.MdiParent = Me
		'Do stuff
 
		.Show()
	 End With
	End If
 
Last edited:
yes, i was just about to post a code section very similar to that, glad ta know you got it working though :)
 
I tried that code out, but it still would not work
But I found a solution:
VB.NET:
	Dim frm As New frmCategorie
    	Dim form As Form
    	Dim blnFound As Boolean = False
    
    	'Is the form already opened?
    	For Each form In Me.MdiChildren
    	  If form.Name = "frmCategorie" Then
    		'If it is found, change some things
    		[b]frm = CType(form, frmCategorie)[/b]
    		frm.Text = "State 2"
    		frm.lblHoofd.Text = "It works!!"
    		frm.MdiParent = Me
    		form.Activate()
    		blnFound = True
    	  End If
    	Next
    
    	If blnFound = False Then
    	  frm.Text = "State 1"
    	  frm.lblHoofd.Text = "Testing"
    	  frm.MdiParent = Me
    	  frm.Show()
    	End If

And this works perfectly
 
Last edited:
You first code posting should work if you make n (As frmTest) a class variable (declare it outside of any procedures using Private n as frmTest).
 
Back
Top