Load a MDI-child by only passing the name
If you want your programmers to only write one line of code to load an MDI-child, you can use the code below to check if the form is already loaded. If not, the form will be created based on the name passed as parameter (this string is case-sensitive!). Put this code in your MDI-parent.
Create this function to see if a form is already loaded:
Private Function FormAlreadyLoaded(ByVal formName As String) As Form
For Each frm As Form In Me.MdiChildren
If frm.Name.ToUpper.Equals(formName.ToUpper) Then
Return frm
End If
Next
Return Nothing
End Function
Add this function to load the form:
Private Sub LoadForm(ByVal formToLoad As String)
Dim frm As Form = FormAlreadyLoaded(formToLoad)
If frm Is Nothing Then
frm = DirectCast(Reflection.Assembly.GetExecutingAssembly.CreateInstance(Reflection.Assembly.GetExecutingAssembly.GetName.Name & "." & formToLoad), Form)
frm.MdiParent = Me
frm.Icon = Me.Icon
frm.Show()
frm.BringToFront()
Else
If frm.WindowState = FormWindowState.Minimized Then
frm.WindowState = FormWindowState.Normal
End If
frm.BringToFront()
End If
Now you can open a form named Form1 by typing LoadForm("Form1")...
Create this function to see if a form is already loaded:
Private Function FormAlreadyLoaded(ByVal formName As String) As Form
For Each frm As Form In Me.MdiChildren
If frm.Name.ToUpper.Equals(formName.ToUpper) Then
Return frm
End If
Next
Return Nothing
End Function
Add this function to load the form:
Private Sub LoadForm(ByVal formToLoad As String)
Dim frm As Form = FormAlreadyLoaded(formToLoad)
If frm Is Nothing Then
frm = DirectCast(Reflection.Assembly.GetExecutingAssembly.CreateInstance(Reflection.Assembly.GetExecutingAssembly.GetName.Name & "." & formToLoad), Form)
frm.MdiParent = Me
frm.Icon = Me.Icon
frm.Show()
frm.BringToFront()
Else
If frm.WindowState = FormWindowState.Minimized Then
frm.WindowState = FormWindowState.Normal
End If
frm.BringToFront()
End If
Now you can open a form named Form1 by typing LoadForm("Form1")...
0 Comments:
Post a Comment
<< Home