P.G.Delhiganesh
P.G.Delhiganesh

Reputation: 3

Clearing Panel controls fail to call Form.Close (or) Form.Leave events

What I have done:
I have 3 Forms, in which I have panel in Form_1 and 2 buttons (button_1, button_2).

button_1 click event add the Form_2 in panel. (before if I clear the Panel controls) button_2 click event add the Form_3 in panel. (before if I clear the Panel controls)

What I look for:
In Form_2 and Form_3 i have Form.closing() and Form.Leave() event, but non of this function called.

I have opened Serial Port in Form_2, it fails to close properly.

Here's my code:

public partial class Form_1 : Form
{
    public Form_1()
    {
        InitializeComponent();
    }

    private void button_1_Click(object sender, EventArgs e)
    {
        Form_2 ObjForm_2 = new Form_2();
        panelBody.Controls.Clear();
        panelBody.Refresh();

        if (ObjForm_2.IsDisposed)
        {
            ObjForm_2 = new Form_2();
        }
        ObjForm_2.TopLevel = false;
        ObjForm_2.FormBorderStyle = FormBorderStyle.None;
        ObjForm_2.Dock = DockStyle.Fill;
        panelBody.Controls.Add(ObjForm_2);
        ObjForm_2.Show();
    }

    private void button_1_Click(object sender, EventArgs e)
    {
        Form_3 ObjForm_3 = new Form_3();
        panelBody.Controls.Clear();
        panelBody.Refresh();

        if (ObjForm_3.IsDisposed)
        {
            ObjForm_3 = new Form_3();
        }
        ObjForm_3.TopLevel = false;
        ObjForm_3.FormBorderStyle = FormBorderStyle.None;
        ObjForm_3.Dock = DockStyle.Fill;
        panelBody.Controls.Add(ObjForm_3);
        ObjForm_3.Show();
    }
}

Upvotes: 0

Views: 405

Answers (1)

Baskar John
Baskar John

Reputation: 726

You should Close the form after clear the controls from the panel.

public partial class Form_1 : Form
{
    private Form frmChild;
    public Form_1()
    {
        InitializeComponent();
    }

    private void button_1_Click(object sender, EventArgs e)
    {

        if (frmChild != null)
        {
            frmChild.Close();
            frmChild.Dispose();
        }
        panelBody.Controls.Clear();

        frmChild = new Form_2();
        frmChild.TopLevel = false;
        frmChild.FormBorderStyle = FormBorderStyle.None;
        frmChild.Dock = DockStyle.Fill;
        panelBody.Controls.Add(frmChild);
        frmChild.Show();
    }

    private void button_1_Click(object sender, EventArgs e)
    {
        panelBody.Controls.Clear();

        if (frmChild != null)
        {
            frmChild.Close();
            frmChild.Dispose();
        }
        panelBody.Controls.Clear();

        frmChild = new Form_3();
        frmChild.TopLevel = false;
        frmChild.FormBorderStyle = FormBorderStyle.None;
        frmChild.Dock = DockStyle.Fill;
        panelBody.Controls.Add(frmChild);
        frmChild.Show();
    }
}

Thanks to Jimi the frmChild.Dispose() is moved to before the panelBody.Controls.Clear() for details check the comments

Upvotes: 0

Related Questions