在VB.NET中关闭SplitContainer Panel2中的打开表单

时间:2012-06-04 10:40:41

标签: vb.net

在VB.NET中需要一些帮助,不知道我在哪里做错了

后台:我有一个使用SplitContainer控件的主窗体。分割的PANEL1带有MenuStrip,Panel2用于调用相关的外部表单enter image description here

代码(参见下文):功能ResetSplitContainerPanel2清除Panel2并使用SetFormAttributesToLoadInPanel2

加载新表单

问题虽然SettingSplitContainer.Panel2.Controls.Clear()清除了Panel2,但表单仍然以可编辑模式维护表单。如果我再次调用相同的表单,我可以看到我之前输入的值

预期输出:在加载新表单时,PANEL2中之前加载的表单应该完全处理

Private Sub ResetSplitContainerPanel2()
    SettingSplitContainer.Panel2.Controls.Clear()
End Sub

Private Function SetFormAttributesToLoadInPanel2(ByVal formNameToChange As Form) As Boolean
        On Error GoTo errHandler

        formNameToChange.IsMdiContainer = False
        formNameToChange.ShowInTaskbar = False
        formNameToChange.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        formNameToChange.ControlBox = False
        formNameToChange.TopLevel = False
        formNameToChange.Text = ""
        formNameToChange.Visible = True
        formNameToChange.Width = SettingSplitContainer.Panel2.Width
        formNameToChange.Height = SettingSplitContainer.Panel2.Height

        SetFormAttributesToLoadInPanel2 = False
        Exit Function

errHandler:
        MsgBox("Error Description: " & Err.Description, vbOKOnly, "Error")
        SetFormAttributesToLoadInPanel2 = True
        Exit Function
    End Function

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

我将尝试使用dispose方法而不是Clear:

Dim f As Form = TryCast(SettingSplitContainer.Panel2.Controls(0), Form)
if f IsNot Nothing then
   f.Dispose()
Endif

不确定您的表单是否已作为Panel2.Controls集合中的第一个控件添加到SplitContainer中。然而,这只是检查。

此更改的根本原因可在this answer

中找到

答案 1 :(得分:0)

你也可以删除Panel2并将该区域留空以查看Form1并使Form1为MdiContainer = True。

然后,对于要在其中打开的每个表单,请使用

form2.mdiparent = form1

之后你只需要使用一个简单的form2.show()。