按钮数组:更改属性

ie

我有一系列的按钮,像这样:

int x = 0, y = 0;
butt2 = new Button[100];

for (int i = 0; i < 100; i++)
{
    butt2[i] = new Button();
    int names = i;
    butt2[i].Name = "b2" + names.ToString();
    butt2[i].Location = new Point(525 + (x * 31), 70 + (y * 21));
    butt2[i].Visible = true;
    butt2[i].Size = new Size(30, 20);
    butt2[i].Click += new EventHandler(butt2_2_Click); //problem lies here (1)
    this.Controls.Add(butt2[i]);
}

private void butt2_2_Click(object sender, EventArgs e)
{
    // want code here
}

我想在单击时更改按钮的背景色。我当时想通过i能够做到这一点:

butt2[i].BackColor = Color.Green;
嵌套循环

这应该可以解决问题:

private void butt2_2_Click(object sender, EventArgs e) 
{
  Button pushedBtn = sender as Button;
  if(pushedBtn != null)
  {
     pushedBtn.BackColor = Color.Green;
  }  
}

对于大多数UI事件,这都适用,“对象发送者”参数是指“发送” /“触发”事件的控件。

要了解有关C#事件处理的更多信息,我将从这里开始

另外,这是一个有关GUI事件处理的SO问题,Juliet很好地回答了这个问题(接受的答案)。

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章