C#按钮循环

本文关键字:循环 按钮 | 更新日期: 2023-09-27 18:27:29

我正试图找出一个应该根据我的书编写的代码(首先进入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.
                }
            }
        }
    }
}

现在它只在红色上循环。我不明白我做错了什么。这不是我写的第一个代码。我从整数到布尔,试图改变颜色,但它要么:只改变一次颜色,不改变其他颜色。或者程序在无限循环时冻结。

C#按钮循环

您不需要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选中,则不存在中断条件。它将运行无限循环并冻结程序。最好在每行CCD_ 2之后添加CCD_。

您不需要这两个while语句。您正在编写一个事件例程,该例程在每次单击按钮1时都会运行。只需设置属性或显示一条消息,然后返回调用该例程的"事件处理器"。

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.

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

您也可以这样做:

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