问题描述
好的,我知道如何"IF"作品。我得到了如何嵌套它们。
Okay, so, I get how "IF" works. And I get how to nest them.
我想做的是在我的表单上有一个按钮,当你点击它时,它会看起来在几个IF。像这样:
What I wanna do is have a button on my form that when you click it, it will look at several IFs. Like this:
namespace buttonTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public bool box1;
public bool box2;
public bool box3;
public bool box4;
public bool box5;
public bool box6;
public bool box7;
public string ret1;
public string ret2;
public string ret3;
public string ret4;
public string ret5;
public string ret6;
public string ret7;
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (box1 == true)
{
Console.WriteLine(ret1);
}
其他
{
Console.WriteLine(" B1 off");
Console.WriteLine("B1 off");
}
if(box2 == true)
{
Console.WriteLine(ret2);
}
其他
{
Console.WriteLine(" B2 off");
}
if(box3 == true)
{
Console.WriteLine(ret3);
}
else
{
Console.WriteLine(" B3 off");
}
if(box4 == true)
{
Console.WriteLine(ret4);
}
else
{
Console.WriteLine(" B4 off");
}
if(box5 == true)
{
Console.WriteLine(ret5);
}
else
{
Console.WriteLine(" B5 off");
}
if(box6 == true)
{
Console.WriteLine(ret6);
}
else
{
Console.WriteLine(" B6 off");
}
if(box7 == true)
{
Console.WriteLine(ret7);
}
else
{
Console.WriteLine(" B7 off");
}
}
private void checkBox1_CheckedChanged(object sender,EventArgs e)
{
box1 = true;
ret1 =" Box1" ;;
}
private void checkBox2_CheckedChanged(object sender,EventArgs e)
{
box2 = true;
ret2 =" Bizzox2" ;;
}
private void checkBox3_CheckedChanged(object sender,EventArgs e)
{
box3 = true;
ret3 =" B-b-b-box3" ;;
}
private void checkBox4_CheckedChanged(object sender,EventArgs e)
{
box4 = true;
ret4 ="哦,这是box4" ;;
}
private void checkBox5_CheckedChanged(object sender,EventArgs e)
{
box5 = true;
ret5 ="这里是numbah 5" ;;
}
private void checkBox6_CheckedChanged(object sender,EventArgs e)
{
box6 = true;
ret6 =" BOXXX 6" ;;
}
private void checkBox7_CheckedChanged(object sender,EventArgs e)
{
box7 = true;
ret7 =" Beeee7teen bomber" ;;
}
}
}
} if (box2 == true) { Console.WriteLine(ret2); } else { Console.WriteLine("B2 off"); } if (box3 == true) { Console.WriteLine(ret3); } else { Console.WriteLine("B3 off"); } if (box4 == true) { Console.WriteLine(ret4); } else { Console.WriteLine("B4 off"); } if (box5 == true) { Console.WriteLine(ret5); } else { Console.WriteLine("B5 off"); } if (box6 == true) { Console.WriteLine(ret6); } else { Console.WriteLine("B6 off"); } if (box7 == true) { Console.WriteLine(ret7); } else { Console.WriteLine("B7 off"); } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { box1 = true; ret1 = "Box1"; } private void checkBox2_CheckedChanged(object sender, EventArgs e) { box2 = true; ret2 = "Bizzox2"; } private void checkBox3_CheckedChanged(object sender, EventArgs e) { box3 = true; ret3 = "B-b-b-box3"; } private void checkBox4_CheckedChanged(object sender, EventArgs e) { box4 = true; ret4 = "Oh this is box4"; } private void checkBox5_CheckedChanged(object sender, EventArgs e) { box5 = true; ret5 = "Here's numbah 5"; } private void checkBox6_CheckedChanged(object sender, EventArgs e) { box6 = true; ret6 = "BOXXX 6"; } private void checkBox7_CheckedChanged(object sender, EventArgs e) { box7 = true; ret7 = "Beeee7teen bomber"; } }}
所以当我运行这个程序,我可以让控制台返回box1的东西,但无论我多少检查或取消选中方框2-7,它们总是显示为"关闭"。
So when I run this program, I can get the console to return the box1 stuff, but no matter how much I check or uncheck boxes 2-7, they always show up as "off".
另外,我觉得必须有一个更简单的方法来写出1000 if / else语句。如果你知道,男孩,我很想知道。在这个测试程序中,只有7个复选框,但在我的实际程序中,大约有20个,而且
的数字可能会增长。
Also, I feel like there has got to be an easier way to do this than to write out 1000 if/else statements. If you know it, boy, I'd love to know it. On this test program, there are only 7 checkboxes but on my actual program, there are about 20 and that number may grow.
推荐答案
这篇关于如何在C#中使用IF语句(Windows窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!