Menu - MDI Forms


Open only one form of the same name for the MDI application. This is the code in the parent (menu) form for the application. Here is the main function to prevent multiple instances from opening. 

' The openForm sub will open all the pages in this program and prevent opening more than one form at a time.
' The propety of Application.OpenForms.Item(frmName) will show if the form is opened. By using the debugger, I was able to
' determined which name to use -- the form name without the new.
' If the form is already opened, then the user will see the notice in the main form's status strip. Users do not like 
' message boxes.
' I found that by using the name property, I can refer to the object, and the text property can be used
' to refer to the Title of the form. 

Sub openForm(ByRef frm As Form) ', ByRef frmName As String, Optional ByRef strTitle As String = ""

	Dim frmName As String = frm.Name.ToString()
	Dim frmText As String = frm.Text.ToString()

	' First test for the opened form.
	If IsNothing(Application.OpenForms.Item(frmName)) Then

		' If the InvokeRequired returns true then the form exists without a reference.
		If Not frm.InvokeRequired Then

			' Sadly, before each call to this sub, we need to check that the form has a reference.
			If frm.IsDisposed Then
				Me.ToolStripStatusLabel1.Text = "Oops, there is an error. I do not know what caused it."
				Exit Sub
			End If

			'Me.ToolStripStatusLabel1.Text = "Oops, there is an error. This should not have happened."
			'Exit Sub

		End If

		frm.MdiParent = Me
		frm.Show()

		Me.ToolStripStatusLabel1.Text = OPENED & frmText

	Else

		Me.ToolStripStatusLabel1.Text = frmText & ISOPENED

	End If

End Sub

The above function is called from a menu item such as Units:

Private Sub UnitsToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnitsToolStripMenuItem1.Click

	If NewUnits.IsDisposed Then
		NewUnits = New Units()
	End If

	openForm(NewUnits)
   
End Sub

The New Units is declared after the class declaration:

 Public Class EstimatorParent Dim NewUnits As New Units()