简体   繁体   English

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

[英]Close opened form in SplitContainer Panel2 in VB.NET

Need some help in the VB.NET, not sure where I am doing wrong 在VB.NET中需要一些帮助,不确定我在哪里做错了

Background: I have a master form which uses the SplitContainer control. 背景:我有一个使用SplitContainer控件的主窗体。 The PANEL1 of the split carries the MenuStrip and Panel2 is used to call related external forms 拆分的PANEL1带有MenuStrip,Panel2用于调用相关的外部表单 在此处输入图片说明

Code (refer below): Function ResetSplitContainerPanel2 clears the Panel2 and loads the new form using the SetFormAttributesToLoadInPanel2 代码(请参见下文):函数ResetSplitContainerPanel2清除Panel2并使用SetFormAttributesToLoadInPanel2加载新表单

Issue: Although SettingSplitContainer.Panel2.Controls.Clear() clears the Panel2 but the form still maintains the form in the editable mode. 问题:尽管SettingSplitContainer.Panel2.Controls.Clear()清除了Panel2,但是窗体仍将窗体保持在可编辑模式。 If I call the same form again, I can see the values which I typed earlier 如果我再次调用相同的表单,则可以看到我之前键入的值

Output Expected: On Load of new form, the previous loaded form in PANEL2 should be disposed completely 预期的输出:在加载新表单时,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

Appreciate your help 感谢您的帮助

I will try to use the dispose method instead of Clear: 我将尝试使用dispose方法而不是Clear方法:

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

Not sure if your form has been added to the SplitContainer as the first control in the Panel2.Controls collection. 不知道您的窗体是否已作为Panel2.Controls集合中的第一个控件添加到SplitContainer中。 However this will be simply to check. 但这只是检查。

The underlying reason for this change could be found in this answer 可以在此答案中找到此更改的根本原因

You could also remove Panel2 and leave that area blank to view Form1 and make Form1 is MdiContainer = True. 您也可以删除Panel2并将该区域留空以查看Form1,并使Form1为MdiContainer = True。

Then for each form you want to open in it use 然后针对您要在其中打开的每个表单使用

form2.mdiparent = form1

After that all you'll have to use is a simple form2.show(). 之后,您只需要使用一个简单的form2.show()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM