C#中form1和form2之间传递数据

Passing data between form1 and form2 in C#

我应该为孩子们制作一个数学练习程序。他们应该能够选择 1 个运算和数字的位数(1、2 或 3 位)。然后它必须根据孩子的选择随机出 10 个问题,然后一旦他们完成了测验,它应该显示他们的结果以及他们错了哪些问题。

我在 form1、操作和数字 # 上进行了两个选择,它们被分配了数字 (1. (*) 2. (/) 3. (+) 4. (-))。我需要做的就是将操作编号和位数传递给 form2,问题将在其中生成并显示。

到目前为止,这是我的 form1 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalProject
{
public partial class Form1 : Form
{
   public static int operation = 0;
    public static int digits = 0;

    public Form1()
    {
        InitializeComponent();
    }
    // this is to make sure only one box is checked for both selections. Starts here
    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void MulCB_CheckedChanged(object sender, EventArgs e)
    {


        if ( MulCB.Checked == true)
        {
            operation = 1;
            DivCB.Checked = false;
            AddCB.Checked = false;
            SubCB.Checked = false;
        }
    }

    private void DivCB_CheckedChanged(object sender, EventArgs e)
    {
        if (DivCB.Checked == true)
        {
            operation = 2;
            MulCB.Checked = false;
            AddCB.Checked = false;
            SubCB.Checked = false;
        }
    }

    private void AddCB_CheckedChanged(object sender, EventArgs e)
    {
        if (AddCB.Checked == true)
        {
            operation = 3;
            DivCB.Checked = false;
            SubCB.Checked = false;
            MulCB.Checked = false;
        }
    }

    private void SubCB_CheckedChanged(object sender, EventArgs e)
    {
        if (SubCB.Checked == true)
        {
            operation = 4;
            DivCB.Checked = false;
            AddCB.Checked = false;
            MulCB.Checked = false;
        }
    }

    private void oneDCB_CheckedChanged(object sender, EventArgs e)
    {
        if(oneDCB.Checked == true)
        {
            digits = 1;
            twoDCB.Checked = false;
            threeDCB.Checked = false;
        }
    }

    private void twoDCB_CheckedChanged(object sender, EventArgs e)
    {
        if ( twoDCB.Checked == true)
        {
            digits = 2;
            oneDCB.Checked = false;
            threeDCB.Checked = false;
        }
    }

    private void threeDCB_CheckedChanged(object sender, EventArgs e)
    {
        if (threeDCB.Checked == true)
        {
            digits = 3;
            oneDCB.Checked = false;
            twoDCB.Checked = false;
        }
    }
    private void button8_Click(object sender, EventArgs e)
    {
        // operations: 1. (*) 2. (/) 3. (+) 4. (-)
        // digits are as number indicates.



        // Second window popup.
        Form2 settingsForm = new Form2();
        settingsForm.Show();
    }
}
}

这是 form2,几乎是裸体的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalProject
{

public partial class Form2 : Form
{


    public Form2()
    {
        InitializeComponent();
    }

    private void FinishedBtn_Click(object sender, EventArgs e)
    {


    }
}

}

这可能有效。 代码中有注释

工作流 正在创建 class Form2 的新实例并设置两个 public 变量。 Public 意味着可以从 class 外部访问它们(如果需要,请参阅 here)。然后调用方法 Show() 并出现 Form。在 Form2 代码中,public 变量现在具有先前指定的值并且可以使用。

表格 1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalProject
{
public partial class Form1 : Form
{    
    public Form1()
    {
        InitializeComponent();
    }
    // this is to make sure only one box is checked for both selections. Starts here
    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    private void MulCB_CheckedChanged(object sender, EventArgs e)
    {


        if ( MulCB.Checked == true)
        {
            operation = 1;
            DivCB.Checked = false;
            AddCB.Checked = false;
            SubCB.Checked = false;
        }
    }

    private void DivCB_CheckedChanged(object sender, EventArgs e)
    {
        if (DivCB.Checked == true)
        {
            operation = 2;
            MulCB.Checked = false;
            AddCB.Checked = false;
            SubCB.Checked = false;
        }
    }

    private void AddCB_CheckedChanged(object sender, EventArgs e)
    {
        if (AddCB.Checked == true)
        {
            operation = 3;
            DivCB.Checked = false;
            SubCB.Checked = false;
            MulCB.Checked = false;
        }
    }

    private void SubCB_CheckedChanged(object sender, EventArgs e)
    {
        if (SubCB.Checked == true)
        {
            operation = 4;
            DivCB.Checked = false;
            AddCB.Checked = false;
            MulCB.Checked = false;
        }
    }

    private void oneDCB_CheckedChanged(object sender, EventArgs e)
    {
        if(oneDCB.Checked == true)
        {
            digits = 1;
            twoDCB.Checked = false;
            threeDCB.Checked = false;
        }
    }

    private void twoDCB_CheckedChanged(object sender, EventArgs e)
    {
        if ( twoDCB.Checked == true)
        {
            digits = 2;
            oneDCB.Checked = false;
            threeDCB.Checked = false;
        }
    }

    private void threeDCB_CheckedChanged(object sender, EventArgs e)
    {
        if (threeDCB.Checked == true)
        {
            digits = 3;
            oneDCB.Checked = false;
            twoDCB.Checked = false;
        }
    }
    private void button8_Click(object sender, EventArgs e)
    {
        // operations: 1. (*) 2. (/) 3. (+) 4. (-)
        // digits are as number indicates.



        // Second window popup.
        // it's the question form, right?
        Form2 questionForm = new Form2();
        //"Write" your settings in the other form's variables
        //You will have to write code that finds out which checkbox is which number! For now its fixed.
        questionForm.operation = 2;
        questionForm.digits = 1;
        questionForm.Show();
        //Hide Form1
        this.Hide();
    }
}
}

Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FinalProject
{

public partial class Form2 : Form
{
    public static int operation;
    public static int digits;


    public Form2()
    {
        InitializeComponent();

    }

    //do NOT paste this. It can be added by creating an event handler
    // you also might not need this, but this method is called when this Form appears. It's an example.
    // https://msdn.microsoft.com/en-us/library/zwwsdtbk(v=vs.80).aspx
    private void Form2_Load(object sender, EventArgs e)
    {
       //here you can use your variables for example (also anywhere within this class!)
       //e.g.
       Textbox1.Text = (string)operation;   
    }

    private void FinishedBtn_Click(object sender, EventArgs e)
    {


    }
}
}