MDI Form - checking if child is already open

Arg81

Well-known member
Joined
Mar 11, 2005
Messages
949
Location
Midlands, UK
Programming Experience
1-3
I have a main form as my MDI Container, and on my menu there is curently a choice of about 10 forms to open.

Knowing what users are like :p , can anyone help with telling me a way of displaying a message or something that if it sees the form is already open, it informs the user instead of creating a new instance of the form?

thanks!
Luke
 
There are many ways how you can do that. For instance you can go for OOP Design Patterns and the Singleton Design Pattern or you can go for function like this one:
PHP:
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
Also you can declare public variable and check if it's true/false (true - it'd mean that certain form is open)
i.e.
PHP:
 If myVar1 = False Then 
myVar1 = True
Dim nForm as form = new form1
nform.mdiparent = Me
nform.show()
End if


Cheers ;)
 
I had this exact question myself not too long ago. Here's how I managed to finally solve it:

PHP:
PublicSharedFunction FindForm(ByVal frmName AsString, ByVal rent As Form) As Form
Dim childNames AsString
Dim children As Form
Dim frmFound As Form = Nothing
If rent.HasChildren() Then
ForEach children In rent.MdiChildren
If children.Name = frmName Then
frmFound = children
EndIf
Next
Else
frmFound = Nothing
EndIf
Return frmFound
EndFunction

This will return a reference to the open form (if it is indeed open).. so in your menu, you could use something along the lines of:

PHP:
Dim childForm as Form = FindForm(findform("StringOfFormName",Me
if IsNothing(childForm) then' Me refers to the parent form
'Do something like showing the form?
childForm.Show()
'Or perhaps just tell the user it's open already
MessageBox.Show("That form is already open, silly!")
End If


But yeah...that's what I did. :D I also have code to dynamically create forms based on a text string (which is coming from a treeview ... IMO, it's kinda cool. :D)


Just realized I neglected to thank some people for their help with above code.

Many thanks to various individuals on various forums for assisting me on above code. jmc was definately one of the exremely helpful individuals. Thanks again! :D
 
Last edited:
OK, I've tried to implement this.
I've changed the publicsharedfunction to a Friend Function in my Global Variables module.

On my main menu, I've used the following code:

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] mnuUserManage_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] mnuUserManage.Click

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] childForm [/size][size=2][color=#0000ff]As[/color][/size][size=2] Form = FindForm(FindForm("frmUserMaintenance", [/size][size=2][color=#0000ff]Me[/color][/size][size=2]))

[/size][size=2][color=#0000ff]If[/color][/size][size=2] IsNothing(childForm) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]childForm.MdiParent = [/size][size=2][color=#0000ff]Me

[/color][/size][size=2]childForm.Show()

[/size][size=2][color=#0000ff]Else

[/color][/size][size=2]MessageBox.Show("That form is already open")

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]
[/color][/size]

But FindForm on the first line comes up with an error;

Class 'System.Windows.Forms.Form' could not be indexed because it has no default property.

Any ideas?

cheers
Luke
 
Sorry about not seeing your post earlier. I have a mistake on the code that I pasted. It should be Dim foundForm as form = FindForm(childName,parentForm)

Not FindForm(findform())

So in your implementation, it would read FindForm("frmUserMaintenance",Me)

HTH

Kaleb
 
No no ... if you want to use my code you should use it as it follows:
VB.NET:
If [color=#0000bb]CheckIfOpen[/color][color=#007700]([/color][color=#0000bb][color=#000000]"frmUserMaintenance"[/color][/color][color=#007700]) [/color][color=blue][color=black]=[/color] False Then[/color]
[color=#007700][color=#000000][color=blue]Dim[/color] nForm [color=blue]as[/color] form = new frmUserMaintenance[/color][/color]
[color=#007700][color=#000000]nForm.show()[/color][/color]
[color=#007700][color=#000000][color=blue]End if[/color]
[/color][/color]

Cheers ;)
 
kulrom said:
No no ... if you want to use my code you should use it as it follows:
VB.NET:
If [color=#0000bb]CheckIfOpen[/color][color=#007700]([/color][color=#0000bb][color=#000000]"frmUserMaintenance"[/color][/color][color=#007700]) [/color][color=blue][color=black]=[/color] False Then[/color]
[color=#007700][color=#000000][color=blue]Dim[/color] nForm [color=blue]as[/color] form = new frmUserMaintenance[/color][/color]
[color=#007700][color=#000000]nForm.show()[/color][/color]
[color=#007700][color=#000000][color=blue]End if[/color]
[/color][/color]

Cheers ;)

But Kul... I said... in the code that I pasted... not you. ;) I'm talking about my implementation (which is what he was trying to use, by the looks of it)
 
Kulrom, I used Kaleb's example as it made more sense to me - no offense meant. I did try your examples but as a neewb in a lot of this it kinda went straight over my head, but I understood Kaleb's example and what it does.

Kaleb - I've changed the code but it still has that error message, on the main form I have:

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] mnuUserManage_Click([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] mnuUserManage.Click

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] childform [/size][size=2][color=#0000ff]As[/color][/size][size=2] Form = FindForm("frmUserMaintenance", [/size][size=2][color=#0000ff]Me[/color][/size][size=2])

[/size][size=2][color=#0000ff]If[/color][/size][size=2] IsNothing(childForm) [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]childForm.MdiParent = [/size][size=2][color=#0000ff]Me

[/color][/size][size=2]childForm.Show()

[/size][size=2][color=#0000ff]Else

[/color][/size][size=2]MessageBox.Show("That form is already open")

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff][/color][/size] 
[size=2][color=#0000ff]
[/color][/size]

Any ideas?

Cheers
Luke
 
The findform function that we've currently implemented will tell us if the form we're trying to open is already open (giving us the option to show it instead of opening a new form).

But if it's not found, we're trying to show something we haven't instantiated yet. I did some scouring on the web and forums, and was able to put something together that allows for the creation of an object based on a string, and then we can cast that object as a form type and open it that way... send me a message with your email and I'll send you the code for this entire thing (rather then spam it here).

Alternatively (to see if it's working), you could do this:

VB.NET:
If IsNothing(FoundForm) Then
childForm = new frmUserMaintenance
childForm.MdiParent = me
childForm.show
end if

This will tell you if the foundform is working... I had to take my code one step further to dynamically create the form object based on the same string as I was doing the FindForm on.

Learned alot, and still learning!! :D
HTH
 
Back
Top