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

时间:2017-05-07 20:45:10

标签: c# winforms transfer

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


    }
}

}

1 个答案:

答案 0 :(得分:0)

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

工作流正在创建类Form2的新实例并设置两个公共变量。公共意味着可以从课外访问它们(如果需要,请参阅here)。然后调用方法Show()并显示表单。在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 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)
    {


    }
}
}