这种情况总是很有趣,因为我们不知道你在课堂上学过什么或没学过什么。因此,根据我上面的评论,您似乎给出了相互矛盾的答案,我将假设 Form2 是在 Form1 的构造函数中创建的,就像这样。您将需要某种方法来确定检查了哪些 Combobox,我只是对二进制值进行异或运算,正如我在评论中所说,我将使用 ShowDialog 启动 Form2 并查看返回值以确定是退出还是继续。我会给你一个快速而肮脏的例子,由你来充实它。如果不是这种情况,您将需要发布更多代码
表格1
public partial class Form1 : Form
{
char[] operators;
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2();
if (frm2.ShowDialog() == DialogResult.OK) //Check for DialogResult Here
{
operators = CreateArray(frm2.GetOperators); // Get ComboBox Values from Form2 and Process them
frm2.Close(); // Close Form2
}
else
Application.Exit(); // If DialogResult is not OK then exit Form
}
private char[] CreateArray( int value)
{
string num = "";
if ((value & 1) == 1)
num += "+";
if ((value & 2) == 2)
num += "-";
if ((value & 4) == 4)
num += "*";
if ((value & 8) == 8)
num += "/";
if ((value & 16) == 16)
num += "%";
return num.ToCharArray();
}
}
表格2
public partial class Form2 : Form
{
int operators;
public Form2()
{
InitializeComponent();
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
//Method that I used for determining CheckBox State. You can use a boolean array or an Enumeration ....
if (cb.Text == "Addition")
operators = operators ^ 1;
else if (cb.Text == "Subtraction")
operators = operators ^ 2;
else if (cb.Text == "Multiplication")
operators = operators ^ 4;
else if (cb.Text == "Division")
operators = operators ^ 8;
else if (cb.Text == "Modulus")
operators = operators ^ 16;
}
public int GetOperators //Property for return value to Form1
{
get { return operators; }
}
}