【问题标题】:Close opened form in SplitContainer Panel2 in VB.NET在 VB.NET 的 SplitContainer Panel2 中关闭打开的表单
【发布时间】:2012-06-04 10:40:40
【问题描述】:

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

背景:我有一个使用 SplitContainer 控件的主窗体。拆分的PANEL1带有MenuStrip,Panel2用于调用相关的外部表单

代码(见下文):函数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

感谢您的帮助

【问题讨论】:

    标签: vb.net


    【解决方案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找到

    【讨论】:

    • 嗨,史蒂夫,主表单中的 panel2 加载时没有外部表单。仅当用户单击该项目时。我尝试了上面的代码,但在 TryCast(SettingSplitContainer.Panel2.Controls(0), Form) 处出现错误,索引 0 超出范围。可能是因为默认情况下,加载主表单时 panel2 为空
    【解决方案2】:

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

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

    form2.mdiparent = form1
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      相关资源
      最近更新 更多