C# 用按钮打开表单

C# open forms with button

我想问这个。我有一个表格这个表格可以控制4个以上的表格。当我点击 "open form 1" 按钮时没问题,但是当我点击 "open form 2 " 时我遇到了一些问题。

我的代码像

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

    }

错误标记 = System.ObjectDisposedException(点击按钮 2 后点击按钮 1)

当您 Close 表单时,表单对象会被释放,因此您将无法对已释放的对象调用 Show。了解 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.

您应该在单击按钮时使用 Hide 方法而不是 Close 方法,这只会对用户隐藏表单。修改你的函数如下:

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

    ac.Show();

}

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

    ac2.Show();

}

按如下方式在按钮单击处理程序上创建新的表单实例:

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();
}

如果有人想使用更多表单可以使用该代码块...

 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";
    }