C# 按钮循环?

3
我正在尝试了解根据我的书(Head first into C #,3.5版)编写的代码。我对我应该编写的循环完全感到困惑。这是我应该做的:

制作一个表单,有一个按钮,复选框和标签。只有当选中复选框时,才会在按下按钮时更改标签的背景颜色。颜色在按下按钮时应在红色和蓝色之间切换。

这是我的当前代码。

namespace SecondColorChangingWindow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            while (checkBox1.Checked == false) // The code will stop if the box isn't checked
            {
                MessageBox.Show("You need the check box checked first!");
                break;//Stops the infinite loop
            }
            while (checkBox1.Checked == true)// The code continues "if" the box is checked.
            {
                bool isRed = false; // Makes "isRed" true, since the background color is default to red.

                if (isRed == true) // If the back ground color is red, this will change it to blue
                {
                    label1.BackColor = Color.Blue; // changes the background color to blue
                    isRed = false; //Makes "isRed" false so that the next time a check is made, it skips this while loop
                    MessageBox.Show("The color is blue.");//Stops the program so I can see the color change
                }
                if (isRed == false)//if the back ground color is blue, this will change it to red
                {
                    label1.BackColor = Color.Red;//Makes the background color red
                    isRed = true;//Sets the "isRed" to true
                    MessageBox.Show("The color is red.");//Stops the program so I can see the color change.
                }
            }
        }
    }
}

现在它只会循环红色。我不明白我做错了什么。这不是我写的第一段代码。我从整数到布尔值尝试改变颜色,但要么只能让颜色变一次,要么程序无限循环而卡住。


3
使用 while 循环会导致程序一直卡住,因为该循环没有退出条件。当你点击复选框使它变为 checked = false 时,循环会运行超过一百万次并在运行时冻结程序。 - SSpoke
3个回答

7

你不需要使用while循环,可以尝试使用以下if条件语句并检查标签颜色:

   private void button1_Click(object sender, EventArgs e)
    {
        if(!checkBox1.Checked)
        {
            MessageBox.Show("You need the check box checked first!");
        }
        else
        {
            //changes the background color
            label1.BackColor = label1.BackColor == Color.Blue? Color.Red:Color.Blue;
            MessageBox.Show("The color is " + label1.BackColor.ToString());
        }
    }

在您当前的代码中,如果 checkBox1 被选中,没有终止循环的条件,则会导致无限循环并冻结程序。最好在每个 MessageBox.Show 行后添加 break;


我非常感激这个。尽管它有效,但书中从未教过我这里写的任何内容。是否可能将其转换为仅if/else语句? - Corey

0

你不需要这两个while语句。你正在编写一个事件程序,每次点击button1时都会运行。只需设置属性或显示消息并返回调用此例程的“事件处理器”即可。

private void button1_Click(object sender, EventArgs e)
{
    if (checkBox1.Checked == false) // The code will stop if the box isn't checked
    {
        MessageBox.Show("You need the check box checked first!");
    }
    else //(checkBox1.Checked == true)
    { // etc.

大多数事件例程将执行一个功能,然后返回到调用者。


0
你也可以这样做:
    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)
        {

            if (label1.BackColor == Color.Blue)
            {
                label1.BackColor = Color.Red;
                MessageBox.Show("The color is red.");
            }
            else
            {
                label1.BackColor = Color.Blue;
                MessageBox.Show("The color is blue.");
            }
        }
        else
        {
            MessageBox.Show("You need the check box checked first!");
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接