C#中form1和form2之间的数据传递 [英] Passing data between form1 and form2 in C#

查看:69
本文介绍了C#中form1和form2之间的数据传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm supposed to make a math practice program for kids. They should be able to choose 1 operation and the amount of digits (1, 2 or 3 digit) the numbers will have. It then has to out put 10 random questions according to the selection made by the kid, then once they have completed the quiz, it should display their results and which questions they got wrong.

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

I have two selections being made on form1, operations and # of digits, which are assigned numbers (1. (*) 2. (/) 3. (+) 4. (-)). All i need to do is communicate the operation number and # of digits to form2, where the questions will be generated and displayed.

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

Here's my code for form1 so far:

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,几乎全裸.

Here's form2, naked pretty much.

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


    }
}

}

推荐答案

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

This might work. There are comments in the code.

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

The workflow is creating a new instance of the class Form2 and setting two public variables. Public means that they can be accessed from outside of the class (see here, if you want). Then the method Show() is called and the Form appears. In the Form2 code, the public variables now have the values previously specified and can be used.

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

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


    }
}
}

这篇关于C#中form1和form2之间的数据传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆