Saturday, April 17, 2010

Single form instance check for MDI Apps in VB.net

I was trying to find a way to have a single instance of a form loaded every time i click on a menu button in my MDI Form. After a few times of modifying my VB.net code i cam up with the following:

'Check if MDI Form have children
If Me.HasChildren Then
'Loop through all the mdi child and check the type of form if it matches
'the type of the form that you wanted to have only 1 instance then
'move the focus to that child form and exit


For Each ChildForm As Form In Me.MdiChildren
If TypeOf ChildForm Is frmSystemSettings Then
ChildForm.Focus()
Exit Sub
End If
Next

End If

'No instance of the form was found
'create one and add to mdi children
'lastly show the form


Dim SettingsForm As New frmSystemSettings
SettingsForm.MdiParent = Me

SettingsForm.Show()



i added the above code under the Click event of the OptionsToolStripMenuItem which i wanted to load only a single instance of a form

No comments:

Post a Comment