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.

1
  • 3
    using a while loop will always freeze the program since that while loop is a loop that has no exit.. by the time you click the checkbox to become checked = false the loop will run over a million times and freeze up the program while its running. Commented Aug 13, 2013 at 3:36

3 Answers 3

7

you don't need while loop, try if condition and check the label color as below

   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());
        }
    }

in your current code there is no break condition if the checkBox1 Checked. it will run infinite loop and freeze the program. you better add break; after each MessageBox.Show lines.

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate this a lot. The book never taught me anything written here though, even though it works. Is it at all possible to turn this into only a if/else statement?
0

You don't need the two while statements. You are coding an event routine that runs every time button1 is clicked. Just set the attribute or display a message and return to the "event processor" that called this routine.

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.

Most event routines will perform one function and then return to caller.

Comments

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!");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.