C# open forms with button

319 views Asked by At

I want to ask this. I have a form this form can control 4 more forms. When i click on the "open form 1" button it is okay but when i clicked "open form 2 " i getting some problem.

my codes like

public Form1()
    {
        InitializeComponent();
    }

    Form2 ac = new Form2();
    Form3 ac2 = new Form3();


    private void button1_Click(object sender, EventArgs e)
    {
        ac2.Close();

        ac.Show();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        ac.Close();

        ac2.Show();

    }

error tag = System.ObjectDisposedException (BUTTON 1 CLICK AFTER BUTTON 2 CLICK)

2

There are 2 answers

4
ashin On BEST ANSWER

When you Close a form, the form object gets disposed and hence you wouldn't be able to call Show on the disposed object. Read about Form.Close() here.

When a form is closed, all resources created within the object are closed and the form is disposed. You can prevent the closing of a form at run time by handling the Closing event and setting the Cancel property of the CancelEventArgs passed as a parameter to your event handler. If the form you are closing is the startup form of your application, your application ends.

You should be using Hide method instead of Close on clicking the buttons, which will only hide the form from the user. Modify your functions as follows:

private void button1_Click(object sender, EventArgs e)
{
    ac2.Hide();

    ac.Show();

}

private void button2_Click(object sender, EventArgs e)
{
    ac.Hide();

    ac2.Show();

}

OR

Create new instance of form on button click handlers as follows:

private void button1_Click(object sender, EventArgs e)
{
    ac2.Close();
    ac2 = null;

    if(ac == null)
    {
       ac = new Form2();
    }

    ac.Show();
}

private void button2_Click(object sender, EventArgs e)
{
    ac.Close();
    ac = null;

    if(ac2 == null)
    {
       ac2 = new Form3();
    }

    ac2.Show();
}
0
furkan as On

If anybody wants to use more forms can use that code block...

 private void ButtonDashboard_Click(object sender, EventArgs e)
    {
        comeuser.Close();
        comeuser = null;

        comereports.Close();
        comereports = null;

        comeabout.Close();
        comeabout = null;



        if (comedash == null)
        {
            comedash = new DashBoard();
        }


            comedash.TopLevel = false;
            comedash.Dock = DockStyle.Fill;
            comedash.Dock = DockStyle.Fill;
            comedash.Show();
            MainEventPanel.Controls.Add(comedash);












        ButtonDashboard.Textcolor = Color.FromArgb(222, 120, 53);
        ButtonUser.Textcolor = Color.Gainsboro;
        ButtonReports.Textcolor = Color.Gainsboro;
        ButtonAbout.Textcolor = Color.Gainsboro;
        HeaderMain.Text = "Dashboard";













    }
private void ButtonUser_Click(object sender, EventArgs e)
    {

        if (comedash != null)
        {
            comedash.Close();

        }

        if (comereports!=null)
        {
            comereports.Close();

        }

        comereports = null;


        if (comeabout != null)
        {
            comeabout.Close();

        }

        comeabout = null;

        if (comeuser==null)
        {
            comeuser = new UserControl();
        }





            comeuser.TopLevel = false;
            comeuser.Dock = DockStyle.Fill;
            comeuser.Dock = DockStyle.Fill;
            comeuser.Show();
            MainEventPanel.Controls.Add(comeuser);
















        ButtonUser.Textcolor = Color.FromArgb(222, 120, 53);
        ButtonDashboard.Textcolor = Color.Gainsboro;
        ButtonReports.Textcolor = Color.Gainsboro;
        ButtonAbout.Textcolor = Color.Gainsboro;
        HeaderMain.Text = "User Control";










    }
    private void ButtonReports_Click(object sender, EventArgs e)
    {

        comedash.Close();
        comedash = null;

        comeuser.Close();
        comeuser = null;

        comeabout.Close();
        comeabout = null;

        if (comeabout==null)
        {
            comeabout = new About();

        }



        comereports.TopLevel = false;
        comereports.Dock = DockStyle.Fill;
        comereports.Dock = DockStyle.Fill;
        comereports.Show();
        MainEventPanel.Controls.Add(comereports);

        ButtonReports.Textcolor = Color.FromArgb(222, 120, 53);
        ButtonUser.Textcolor = Color.Gainsboro;
        ButtonDashboard.Textcolor = Color.Gainsboro;
        ButtonAbout.Textcolor = Color.Gainsboro;

        HeaderMain.Text = "Reports";
    }