1. 在窗体的构造器New()方法中,添加以下代码
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'================================================================================================
'将窗体作为子控件显示页面
'---------------------------------------------------------------------------------------------
'窗体作为子控件显示时,'必须在New()方法中设置窗体的TopLevel(顶级窗体) = False。
Me.TopLevel = False
Me.Dock = DockStyle.Fill
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None '将窗体设置为无标题边框窗体
'在窗体上添加自己的标题栏
Me.pnlFrmTitle.Dock = DockStyle.Top
Me.lblFrmTile.Anchor = AnchorStyles.Left
Me.btnFrmClose.Anchor = AnchorStyles.Right
Me.pnlFrmTitle.BackColor = System.Drawing.Color.LightYellow
Me.lblFrmTile.Text = FormTitle
'================================================================================================
End Sub
2. 创建窗体的属性
Private sFormTitle As String = "窗体标题"
''' <summary>
''' 属性 窗体的标题文本
''' </summary>
Public Property FormTitle() As String
Get
Return sFormTitle
End Get
Set(ByVal value As String)
sFormTitle = value
Me.lblFrmTile.Text = value
End Set
End Property
Private bIsShowTitleBar As Boolean = True
''' <summary>
''' 属性 是否显示标题栏
''' </summary>
Public Property IsShowTitleBar() As Boolean
Get
Return bIsShowTitleBar
End Get
Set(ByVal value As Boolean)
bIsShowTitleBar = value
Me.pnlFrmTitle.Visible = value
End Set
End Property
3. 在其他控件中把窗体作为子控件显示
Public Sub LoadFrom()
Me.Panel1.Controls.Clear()
Me.Panel1.Controls.Add(Form1.GetInstance)
Control.Show() '显示控件(窗体)语句,不写无法显示窗体。
End Sub