0

大家好,我正在动态创建一些按钮,当用户单击按钮时,我需要将所选按钮的 BackColor 设置为一些突出显示的颜色,根据我编写的代码,它正在为每个按钮应用颜色,而不是那个我需要为用户单击的按钮应用颜色

我创建按钮的代码如下

for(int i=0;i<5;i++)
{
    Button btyDynamic = new Button();
    btyDynamic .Click += new EventHandler(btyDynamic _Click);
    btyDynamic .AutoSizeMode = AutoSizeMode.GrowAndShrink;
    btyDynamic .AutoSize = true;
    btyDynamic .Text = i.ToString();
    btyDynamic .Tag = i.ToString();;
}

protected void btyDynamic(object sender, EventArgs e)
{
   Button btn= sender as Button;
   string strTag= btn.Tag.ToString();
   switch(strTag)
   {
      case "0":
         btn.BackColor=Color.LightSteelBlue;
       break;
     // Like this I am writing, now when in Case2 I need to remove the color of the first button and to show the backcolor of second button
   }
}
4

6 回答 6

2

您可以存储最后选择的按钮,然后重置BackColor

private Button _lastButtonSelected = null;
protected void btyDynamic(object sender, EventArgs e)
{
   // Set new button back color
   Button btn = sender as Button;
   if(btn != null)
   {
      btn.BackColor = Color.LightSteelBlue;
   }
   // Reset last button color
   if(_lastButtonSelected != null)
   {
      _lastButtonSelected.BackColor = ...; // Put default back color here
   }
   _lastButtonSelected = btn;
}

如果您希望在单击同一个按钮两次BackColor时保留LightSteelBlue,则只需检查_lastButtonSelected != btn

于 2013-07-10T15:20:22.027 回答
1

您可以为所有按钮创建一个 foreach,并为所有不等于发件人的按钮删除背景。假设将所有按钮保存到一个数组 (_AllButtons) 中。你可以写一些这样的代码:

Button btn= sender as Button;
foreach(var currentButton in _AllButtons) {
   if(currentButton !=btn) {
        currentButton.BackColor=Color.Transparent;
   }
}
于 2013-07-10T15:12:35.513 回答
0

你可以试试这个:

    Color highLite = Color.Black;

    public Form1()
    {
        InitializeComponent();
        for (int i = 0; i < 20; i++)
        {
            Button b = new Button();
            b.Text = i.ToString();
            b.Tag = null;
            b.Click += b_Click;
            flowLayoutPanel1.Controls.Add(b);
        }
    }

    void b_Click(object sender, EventArgs e)
    {
        Button b = sender as Button;
        if (b == null)
            return;

        b.BackColor = highLite;

        // clear backcolors
        foreach (Control c in flowLayoutPanel1.Controls)
            if (c != b)
                c.BackColor = SystemColors.Control;
    }
于 2013-07-10T15:20:50.330 回答
0

一种可能的算法是:

  • 像你已经做的那样创建一次按钮并将点击事件绑定到一个处理程序
  • 在点击处理程序中,首先将所有按钮重置为标准背景颜色
  • 在单击处理程序中,使用 switch 找到当前单击的按钮,并设置此按钮的颜色

示例代码:

Action resetButtonColor = () => 
{
    button1.BackColor = Colors.Red;
    button2.BackColor = Colors.Red
    button3.BackColor = Colors.Red;
};

resetButtonColor();

var selected = Colors.Green;
switch(strTag)
{
    case "1": button1.BackColor = selected;
    case "2": button2.BackColor = selected;
    case "3": button3.BackColor = selected;
}
于 2013-07-10T15:18:26.350 回答
0

然后你必须明确地分配给这个按钮的点击事件。所有其他人都可以有这个处理程序,但一个按钮不应该,因为以这种方式处理太通用了。

于 2013-07-10T15:19:20.670 回答
0

确保您只设置要设置的控件的背景色。让标签有一些独特的东西来告诉你你想要改变它。

private Color offColor = Color.Red;
private Color onColor = Color.Blue;
private String btyPrefix = "bty";

private void btyDynamic_click(object sender, EventArgs e)
{
    Control control = (Control)sender;
    // enumerate this.Controls, but if they go into a different container, enumerate over that
    this.Controls.OfType<Control>()
        .Where(c => ((String)c.Tag).Contains(btyPrefix))
        .ToList<Control>()
        .ForEach(c =>
            {
                if (control == c)
                    c.BackColor = onColor;
                else
                    c.BackColor = offColor;
            }
    );
}

private void Form1_Load(object sender, EventArgs e)
{
    Button btyDontChange = new Button();
    btyDontChange.AutoSize = true;
    btyDontChange.AutoSizeMode = AutoSizeMode.GrowAndShrink;
    btyDontChange.Text = "x";
    btyDontChange.Tag = "something";
    btyDontChange.Location = new Point(0, 0);
    this.Controls.Add(btyDontChange);
    for (int i = 0; i < 5; i++)
    {
        Button btyDynamic = new Button();
        btyDynamic.Click += new EventHandler(btyDynamic_click);
        btyDynamic.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        btyDynamic.AutoSize = true;
        btyDynamic.Text = i.ToString();
        btyDynamic.Tag = btyPrefix + i.ToString();
        btyDynamic.Location = new Point((i+1) * 50, 0);
        btyDynamic.BackColor = offColor;
        this.Controls.Add(btyDynamic);
    }
}
于 2013-07-10T16:03:39.750 回答