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



我应该为孩子们做一个数学练习程序。他们应该能够选择 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();
    }
}
}

这是形式2,几乎是裸体。

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)
    {

    }
}

}

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

工作流正在创建类Form2的新实例并设置两个公共变量。公共意味着可以从类外部访问它们(如果需要,请参阅此处(。然后调用方法Show()并显示窗体。在Form2代码中,公共变量现在具有先前指定的值,可以使用。

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

表格2:

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)
    {

    }
}
}

相关内容

  • 没有找到相关文章

最新更新