直接添加一个MID父窗体或在已有窗体的属性中找到IsMDIContainer属性,然后设置为True,然后创建第二个窗体 ,需要加载子窗体的时候:
创新互联建站成立于2013年,先为宕昌等服务建站,宕昌等地企业,进行企业商务咨询服务。为宕昌企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
Dim NewMDIChild As New Form2
NewMDIChild.MdiParent = Me
NewMDIChild.Show()
Public Shared Sub CheckMDIChildForm(ByVal MDIForm As Windows.Forms.Form, ByVal MDIChildForm As Windows.Forms.Form, ByVal MDIChildFormName As String)
If MDIForm.MdiChildren.Length 1 Then
'如果没有任何一个MDI子窗体,则创该MDI子窗体的窗体实例
Dim MDIChildFrm As Windows.Forms.Form = MDIChildForm ' 定义MDI子窗体
MDIChildFrm.MdiParent = MDIForm '指定父窗体
MDIChildFrm.Show() '打开窗体
Exit Sub
Else
Dim x As Integer
Dim frmyn As Boolean
For x = 0 To (MDIForm.MdiChildren.Length) - 1
Dim tempChild As Windows.Forms.Form = CType(MDIForm.MdiChildren(x), Windows.Forms.Form)
If tempChild.Name = MDIChildFormName Then
'检测到有该MDI子窗体,设为激活 并退出循环
frmyn = True
tempChild.BringToFront()
Exit For
Else
frmyn = False
End If
Next
If Not frmyn Then
'在打开的窗体中没检测到则新建
Dim MDIChildFrm As Windows.Forms.Form = MDIChildForm ' 定义MDI子窗体
MDIChildFrm.MdiParent = MDIForm '指定父窗体
MDIChildFrm.Show() '打开窗体
End If
End If
End Sub
这样的话你就不要直接把子窗口show出来,先在父窗口创建一个子窗口对象的变量,通过实例化该变量达到创建子窗口的目的,然后在下次又要打开子窗口时也可以通过该变量关闭原有的子窗口
示例代码如下(假设Form1为父窗口,Form2为子窗口):
Form1的代码:
Public
Class
Form1
Dim
nform
As
Form2
=
Nothing
Private
Sub
Button1_Click(ByVal
sender
As
System.Object,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
If
nform
IsNot
Nothing
Then
nform.Close()
nform.Dispose()
End
If
nform
=
New
Form2
nform.Show()
Me.Hide()
End
Sub
End
Class
通过nform变量,你可以很轻易就处理掉原来已经打开但隐藏着的子窗口了
既然子窗体都有同名函数,就应该对子窗体进行一层封装,子类实现不同方法
Public MustInherit Class AbstractChild
Public MustOverride Sub Method1()
End Class
Public Class ChildClass1
Inherits AbstractChild
Public Overrides Sub Method1()
MessageBox.Show("class1")
End Sub
End Class
Public Class ChildClass2
Inherits AbstractChild
Public Overrides Sub Method1()
MessageBox.Show("class2")
End Sub
End Class
Public Class ChildClass3
Inherits AbstractChild
Public Overrides Sub Method1()
MessageBox.Show("class3")
End Sub
End Class
Dim child1 As New ChildClass1()
Dim child2 As New ChildClass2()
Dim child3 As New ChildClass3()
child1.Method1()
child2.Method1()
child3.Method1()