Dan Ho

Dan Ho

  • NA
  • 12
  • 14.9k

do.. while checkbox is checked??

Jun 16 2011 9:18 AM
Hello,

first off, im a real novice when it comes down to programming so dont wonder if my approach is totally wrong.

I have a CheckBox and want to implement a do while loop that will run as long as the checkbox stays checked.
Once I uncheck the checkbox, it should jump out of the loop and stop.

Since I'm a beginner I really dont know how to approach this. I've tried the following but once in the loop, it will hang and stay in the loop forever even if I uncheck the box and I'll have to shut down the process via task manager, heres the snippet:

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                do
                {
                    textBox1.AppendText("On");

                } while (checkBox1.Checked);
            }
            else if (!checkBox1.Checked)
            {
                textBox1.AppendText("Off");
            }
        }

The AppendText thing makes no sense but its just for testing purposes at the moment to see if I can get it working as I want.

I would be glad if anyone could help me to solve this issue.

Thanks.

Answers (14)