How can i check if one of the child forms is already open as i dont want to create multiple instances of the same form
cheers
Printable View
How can i check if one of the child forms is already open as i dont want to create multiple instances of the same form
cheers
You can use something called Singletons. Only one instance of the class is allowed to exist.
http://www.codeproject.com/vb/net/Si...eton_Forms.asp
since the thread title is called MDI, then i will assume that you are talking about mdi child forms.
check my code for this issue , i use it in my mdi application
VB Code:
' this code should be written in the mdi parent form dim frmx as new form1 'form1 is the name of the wanted form Dim ctl As Form For Each ctl In me.MdiChildren If ctl.Name = FrmX.Name Then ctl.BringToFront() : ctl.Show() FrmX.Dispose() Exit Sub End If Next 'Setting Parent FrmX.MdiParent = me
i hope this will help
rgds
Assuming that the type of the MDI child is Form2, I would use something similar to maged but not quite the same:Edit:VB Code:
Dim myChild As Form2 For Each frm As Form In Me.MdiChildren If TypeOf frm Is Form2 myChild = frm Exit For End If Next frm If myChild Is Nothing Then 'Create a new child form. myChild = New Form2 myChild.MdiParent = Me myChild.Show() Else 'Activate the existing child form. myChild.Activate() End If
Having said that, the Singleton approach is definitely the more professional. It is actually not a bad idea to create a SingletonForm class that inherits the Form class and then derive all the forms you want to have this behaviour from that, instead of from Form as you usually would.
the advantage of your code is that u dont create an new instance of the form if it is not needed. for sure that is more effecient than creating a new form and then disposing it.
better code i must admit ;)
cool, cheers guys, first time doing a mdi app so will prob have more questions later