C# 我的表格1如何知道该复选框已从表格2启用?

C# 我的表格1如何知道该复选框已从表格2启用?,c#,checkbox,C#,Checkbox,这是我的表格2,这是复选框所在的地方 我的操作符在中间,有一个随机字符生成器,我的代码是: char[] select = new char[] { '+', '-', '/', '%', '*' }; var rand = new Random(); char num = select[rand.Next(5)]; lbloperator.Text = Convert.ToString(num); 如果唯一的检查是加法,我的表

这是我的表格2,这是复选框所在的地方

我的操作符在中间,有一个随机字符生成器,我的代码是:

        char[] select = new char[] { '+', '-', '/', '%', '*' };
        var rand = new Random();
        char num = select[rand.Next(5)];
        lbloperator.Text = Convert.ToString(num);
如果唯一的检查是加法,我的表格1将只执行加法,或者如果是加法、减法,我的表格1将只执行加法和减法。请帮我做这个(

表格1


我不太确定您到底在问什么,但听起来您好像在尝试在表单之间传递一些数据

在Form2中,可以添加读取标签的公共属性

public string TheOperator {
    get { return lblOperator.Text; }
}
然后从Form1中,您可以创建form2的新实例,然后在需要时引用该属性

Form2 fm = new Form2();
fm.Show();
string theOp = fm.TheOperator;
//////////

回应您的编辑: 您可以将其添加到Form1中,在其中公开运算符变量:

public string MyOperator {
    get { return lblOperator.Text; }
    set {
                lblOperator.text = value;
                //You can perform any updates to 
                //your calculations here, or call 
                //another method to do so
        }
}

private void OpenForm2()
{
    Form2 frm2 = new Form2(this);
    frm2.Show();
}
然后在Form2内部,传递对Form1的引用,以便访问公共属性:

private Form1 frm;
public New(Form1 _frm)
{
    frm = _frm;
}

private void UpdateOperator()
{
    //call this method, calculating your operator and then
        //set the operator on the first form (variable frm) to
        //What you need it to do
    frm.MyOperator = lblOperator.Text;
}
试试这个:

public partial class Form2: Form
{
public string checkBoxSelected = "";
}


public partial class Form1 : Form
{
private void MakeResult()
{
Form2 result = new Form2();
result.checkBoxSelected = "+";
result.Show();
}
}

有两种方法可以做到这一点:

1-第一种方法是在form2中定义公共属性,它可以告诉form1 checkBox1的状态:

表格2:

public bool MyCheckBoxStatus
{
    get {return checkBox1.Checked;}
}
表格1(例如):

2-另一种方式:如果您使用的是Windows窗体应用程序,则所有控件都有一个名为Modifiers的属性。请将其值更改为public,并在Form1中编写此代码:

Form2 frm = new Form2;
frm.ShowDialog();

if (frm.checkBox1.Checked)
{
    //Do something...
}

很抱歉我的英语不好,我只是想把数据从我的form2传递到form1,如果chkbxAdd被选中,那么在我的form1上,lbloperator将只显示“+”,现在,如果在表单2中,它是全部启用的,那么我的表单1将显示所选的运算符。例如,我全部启用了它们,那么可能的输出将是,1+1,1-1,1*1,1/1,1%1,类似于此。它在这里说方法必须有一个返回类型。public New(form1\u frm)很抱歉,先生,我是新来的。谢谢,很高兴它成功了!但是有一个问题,它是showDialog,所以每次我按下按钮,它都会继续打开。--无论如何,非常感谢!:)如果你想的话,你可以使用Show()方法,但请担心Show()方法会导致:1-Form1不会等待用户响应。2-用户可以返回到Form1而不关闭Form2。
Form2 frm = new Form2;
frm.ShowDialog();

if (frm.checkBox1.Checked)
{
    //Do something...
}