3

I'm trying to figure out a code I'm supposed to write in accordance to my book (Head first into C#, 3.5 edition). I'm just absolutely baffled by a loop I'm suppose to write. Here's what I'm suppose to do:

Make a form, have a button, check box, and label. Only when the check box is marked, is the button suppose to change the background color of the label. The color is suppose to switch between red and blue when the button is pressed.

This is my current code.

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.
                }
            }
        }
    }
}

Right now it only loops on red. I don't understand what I'm doing wrong. This isn't the first code I've written. I've gone from integers to Boolean trying to get the color to change, but it either: makes the color once and no other color. Or the program freezes as it infinite loops.

4

3 回答 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 已选中,则没有中断条件。它将运行无限循环并冻结程序。你最好break;在每MessageBox.Show行之后添加。

于 2013-08-13T03:30:15.297 回答
0

You can do it like that also:

    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!");
        }
    }
于 2013-08-13T03:42:32.793 回答
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.

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

于 2013-08-13T03:30:52.950 回答