我在Form1上使用按钮打开Form2。单击Form2中的另一个按钮,然后将此表单作为控件添加到Form1的拆分容器面板中。之后我关闭表单,但仍然可以看到拆分容器控件(我的Form2)。现在我想执行相反的操作 - 关闭拆分容器控件并像以前一样打开Form2到普通窗口,再次从Form2中的相同按钮,在这种情况下控件在拆分容器的面板中。这可能吗?
我的主要目的是将Form2停靠在拆分容器中并取消停靠它,所有这些都是从Form2上的按钮完成的。这是我的代码:
编辑:
Public Class Form2
Dim BtnState As Integer
Dim frm As Form2
Private Shared Sub DockFormToPanel(panel As Panel, frm As Form)
frm.TopLevel = False
frm.WindowState = FormWindowState.Maximized
frm.FormBorderStyle = FormBorderStyle.None
frm.Visible = True
Form1.SplitContainer1.Panel2.Controls.Add(frm)
End Sub
Private Shared Sub UndockFormFromPanel(panel As Panel, frm As Form)
If Not Form1.SplitContainer1.Panel2.Controls.Contains(frm) Then Throw New InvalidOperationException()
Form1.SplitContainer1.Panel2.Controls.Remove(frm)
frm.Visible = False
frm.TopLevel = True
frm.WindowState = FormWindowState.Normal
frm.FormBorderStyle = FormBorderStyle.Sizable
frm.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If BtnState = 0 Then
If frm Is Nothing Then
frm = New Form2
AddHandler frm.FormClosed, Sub() frm = Nothing
End If
DockFormToPanel(Form1.SplitContainer1.Panel2, frm)
BtnState = 1
Else
UndockFormFromPanel(Form1.SplitContainer1.Panel2, frm)
BtnState = 0
End If
End Sub
End Class
答案 0 :(得分:0)
解决。如果有人有任何更好的解决方案或认为这不是一个好方法,请评论。这是我的最终代码(在Form2中):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form2
If Form1.DataGridView1.Visible = True Then
Form1.DataGridView1.Visible = False
frm.TopLevel = False
Form1.SplitContainer1.Panel2.Controls.Add(frm)
frm.WindowState = FormWindowState.Maximized
frm.FormBorderStyle = FormBorderStyle.None
frm.Show()
Me.Close()
Else
Me.Parent = Nothing
Form1.DataGridView1.Visible = True
frm.TopLevel = True
frm.FormBorderStyle = FormBorderStyle.Sizable
frm.WindowState = FormWindowState.Normal
frm.StartPosition = FormStartPosition.CenterScreen
frm.Show()
End If
End Sub