简体   繁体   中英

Close opened form in SplitContainer Panel2 in VB.NET

Need some help in the VB.NET, not sure where I am doing wrong

Background: I have a master form which uses the SplitContainer control. The PANEL1 of the split carries the MenuStrip and Panel2 is used to call related external forms 在此处输入图片说明

Code (refer below): Function ResetSplitContainerPanel2 clears the Panel2 and loads the new form using the SetFormAttributesToLoadInPanel2

Issue: Although SettingSplitContainer.Panel2.Controls.Clear() clears the Panel2 but the form still maintains the form in the editable mode. 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

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:

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. 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.

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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