单击多个按钮后更改按钮的背景颜色

时间:2014-11-27 23:52:56

标签: c# winforms button background-color

我正在使用Visual C#2010

我有一个带有大约24个按钮的Windows窗体和一个“完成”按钮

目前,“完成”按钮为红色。一旦点击了所有其他按钮,我希望此按钮的颜色变为绿色。

当用户点击按钮1 -24 按钮变为绿色时,我知道如何更改点击事件的背景。

我在点击每个按钮时都有一个布尔值。

这是我到目前为止的代码:

if (button1Clicked && button2Clicked ...)  // I have button3 through button24 as well
{
    finishButton.BackColor = Color.Green;
}

但是,单击所有24个按钮后,按钮仍为红色。 我已经将button1的输出发送到debug writeline,我知道当单击按钮时布尔值从false变为true。 我在修饰符选项中公布了完成按钮。

我错过了一步吗?我认为这应该很容易实现。

编辑


public partial class Form2 : Form
{
    public bool button1Clicked = false;
    .... // and other buttons are allocated the same way
}


private void button1_Click(object sender, EventArgs e)
{
    String textForClipboard = String.Format("{0}_S1_{1}", Form1.dateValue, button1.Text);
    Clipboard.SetText(textForClipboard);
    button1.BackColor = Color.Green;
    button1Clicked = true;
    System.Diagnostics.Debug.WriteLine(button1Clicked);
}

修改


我应该在哪里使用if循环? 我目前在这个方法中有它

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        ....

        public Form2(Form parentForm)
        {
            InitializeComponent();
            mainForm = parentform;
            ....

            if (button1Clicked)
            {
                finishedButton.BackColor = Color.Green;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:3)

这是一个快速的肮脏解决方案,但无论如何都有效。它假设您的24个按钮被命名(不是他们的文本)button1button24

1)将24个按钮放在GroupBox(例如groupBox1

2)将其放在partial class Form2 : Form

bool[] allButtons = new bool[24];

private void button_Click(object sender, EventArgs e)
{
    var indexButton = int.Parse((((Button)sender).Name.Substring(6))) - 1;
    allButtons[indexButton] = true;
    allButtonsClicked();
}

private void allButtonsClicked()
{
    const int totalButtons = 23;
    int counter = 0;
    for (int i = 0; i < totalButtons; i++)
    {
        if (allButtons[i])
        {
            counter++;
        }
    }
    if (counter == totalButtons)
    {
        finishButton.BackColor = Color.Green;
    }
}

3)将其放在Form2_Load()

foreach(Control ctl in groupBox1.Controls)
    {
        if (ctl is Button)
        {
            ctl.Click += button_Click;
        }
    }

它的作用

groupBox1加载时,它会遍历Form2中的所有控件,检查它们是button并将所有24个按钮click事件挂钩到button_click() 。这样您就不需要声明24次按钮,因为每次点击24个按钮时都会触发button_click()

然后我们有一个allButtons bool数组,其中有24个位置,最初都设置为false。每次button_click()触发时,我们都会提取&#34;数字&#34;在该给定时间触发button_click()的按钮名称的一部分(例如,如果它button1在给定时间触发button_click(),我们采用子串{{1}来自"1"),将其转换为int(所以"button1"变为"1")并使用它来设置1(数组从零开始,因此为allButtons[0] = true 1}}用于索引1)。当0的所有24个元素变为allButtons(已在true中签入)时,我们会将allButtonsClicked()的颜色更改为finishButton

答案 1 :(得分:2)

对环球旅行者答案的一个小修改。 将allButtonsClicked()更改为返回bool而不是void

private bool allButtonsClicked()
{
    const int totalButtons = 23;
    int counter = 0;
    for (int i = 0; i < totalButtons; i++)
    {
        if (allButtons[i])
        {
            counter++;
        }
    }
    if (counter == totalButtons)
    {
        return true;
    }
    return false;
}

所以你可以这样做:

private void button_Click(object sender, EventArgs e)
{
    var indexButton = int.Parse((((Button)sender).Name.Substring(6))) - 1;
    allButtons[indexButton] = true;
    if (allButtonsClicked())
    {
        finishButton.BackColor = Color.Green;
    }
}

这只会使代码在日常语言中更具可读性和可理解性:"Whenever a button is clicked, check if each of the 24 buttons has been clicked at least once. If yes, then change the BackColor of finishButton to Green"并将您的逻辑集中在一个地方而不是两个。

答案 2 :(得分:0)

首先,确保条件的内部部分随时执行。 例如,您可以弹出一个消息框。 对于第一个较小的测试用例,只检查3个按钮。

对于其他测试,请移除整个条件并仅使用颜色更改。这有效吗? 如果没有,那么你必须先玩那个部分。

如果它有效,那么回到你的状态,这将是泄密者。首先检查一个按钮点击。然后两个等。

通过这种方式,您可以随时轻松找到答案。在小任务中完成,找到问题。

勾选条件的调试点,并检查哪个标志为false。鼠标悬停在button1上,等等,一切都应该是真的