在c#中的表单之间交替

时间:2017-03-11 16:31:09

标签: c#

我有6个表格。 3表格是表格4-6的形式,这些形式的功能只是一个基本的开放和结束。然后有表格1-3都有菜单条连接到表格4-6。表单1是初始页面,根据您单击的按钮打开表单2或3。表单2在按钮单击时打开表单3,表单3具有打开表单2的选项。

我现在的问题是,每当我从表单3打开表单2时,我都有一个空引用异常。

表格2代码:

    Form3 game = new Form3();
    Form4 oneplayer = new Form4();
    Form5 twoplayer = new Form5();
    Form6 creditpage = new Form6();

    public Form2()
    {
        InitializeComponent();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void onePlayerToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        oneplayer.Show();
    }

    private void twoPlayerToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        twoplayer.Show();
    }

    private void creditToolStripMenuItem_Click(object sender, EventArgs e)
    {
        creditpage.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        game.Show();
    }

表格3代码:

    Form2 choice;
    Form4 oneplayer = new Form4();
    Form5 twoplayer = new Form5();
    Form6 creditpage = new Form6();

    public Form3()
    {
        InitializeComponent();
    }

    private void twoPlayerToolStripMenuItem_Click(object sender, EventArgs e)
    {
        choice.Show();
        this.Close();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void onePlayerToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        oneplayer.Show();
    }

    private void twoPlayerToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        twoplayer.Show();
    }

    private void creditToolStripMenuItem_Click(object sender, EventArgs e)
    {
        creditpage.Show();
    }

新的。请原谅我:P

2 个答案:

答案 0 :(得分:1)

您没有将表单设置为任何内容:

Form2 choice;   // <===== Here
Form4 oneplayer = new Form4();
Form5 twoplayer = new Form5();
Form6 creditpage = new Form6();

答案 1 :(得分:0)

您需要在form3中创建一个方法,如下所示:

 public void setForm2 (Form2 choice_)
    {
        choice = choice_;
    }

并将此行添加到button1.click

game.setForm2(this);