本文介绍了单击按钮时更改多个按钮的颜色该按钮加上相同行和列中相邻的按钮应该更改..如果按钮为红色则变为绿色&如果它是绿色的......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含25个按钮[5乘5]的表单。 On_load所有按钮都变为绿色..如果你点击一个按钮,按钮旁边的按钮,左边,右边,顶部和下边的按钮就会变成相同的颜色。我已经成功了。



现在的问题是,假设你点击说按钮13,按钮12,13,14,8,18应改为让说红色;现在,当你点击说按钮14按钮13,14,15,9,19也应该变为红色但是代码应首先检查一个按钮是否最初是绿色然后它应该变成红色但是如果它是红色那么它应该转回到绿色...所以在btn14 click_event的情况下,按下btn13时点击红色的按钮13和14现在应该变为绿色,当btn14点击.......这里是我的成就的代码片段,即将点击的按钮与相同行和列中相邻的按钮一起更改为相同的颜色。



这是按钮的排列方式

[1] [2] [3] [4] [5]

[6] [7] [8] [9] [1o]

[11] [ 12] [13] [14] [15]

[16] [17] [18] [19] [20]

[21] [22] [23 ] [24] [25]





private void btn13_Click(object sender,EventArgs e){

按钮btn13 =(按钮)发件人;

btn13.BackColor = Color.Red;

btn12.BackColor = Col or.Red;

btn14.BackColor = Color.Red;

btn8.BackColor = Color.Red;

btn18.BackColor = Color。红色;

}



private void btn14_Click(object sender,EventArgs e){

Button btn14 =(按钮)发件人;

btn14.BackColor = Color.Red;

btn13.BackColor = Color.Red;

btn15.BackColor = Color。红色;

btn9.BackColor = Color.Red;

btn19.BackColor = Color.Red;

}

这个代码块被重新设置为所有按钮click_event

...请帮帮我。谢谢!

I created a form with 25 buttons [5 by 5]. On_load all the buttons turn Green..and want that if u click on a button, the button alongside the one to its left, right, top and below to change to the same color. I have succeeded in doing that.

Now the problem is that, supposing u click say button 13, buttons 12, 13, 14, 8, 18 should change to lets say Red; now when u click say button 14 buttons 13, 14, 15, 9, 19 should change to Red too BUT the code should first of all check if a button was initially green then it should turn Red but if it was red then it should turn back to Green...so in the case of btn14 click_event, buttons 13 and 14 that was turn Red when btn13 was click should now turn Green when btn14 is click.......here is a code snippet of my achievement i.e changing the clicked button alongside the button adjacent in the same row and column to the same color.

this is how the buttons are arranged
[1] [2] [3] [4] [5]
[6] [7] [8] [9] [1o]
[11] [12] [13] [14] [15]
[16] [17] [18] [19] [20]
[21] [22] [23] [24] [25]


private void btn13_Click(object sender, EventArgs e){
Button btn13 = (Button)sender;
btn13.BackColor = Color.Red;
btn12.BackColor = Color.Red;
btn14.BackColor = Color.Red;
btn8.BackColor = Color.Red;
btn18.BackColor = Color.Red;
}

private void btn14_Click(object sender, EventArgs e){
Button btn14 = (Button)sender;
btn14.BackColor = Color.Red;
btn13.BackColor = Color.Red;
btn15.BackColor = Color.Red;
btn9.BackColor = Color.Red;
btn19.BackColor = Color.Red;
}
This block of code is repitted for all the button click_event
...Please help me out. Thank you!

推荐答案

private void ChangeButtonColors(object sender, EventArgs e){
  var button = sender as Button;
  ApplyColor(button);

  // find adjacent buttons -> assuming their layout (IDs) is EXACTLY as you have mentioned
  int id = GetIntegerId(button);
  int topId = id - 5; // top
  var top = this.Controls.Find("btn" + topId, true).FirstOrDefault() as Button;
  if(top != null)
    ApplyColor(top);

  int bottomId = id + 5; // bottom
  var bottom = this.Controls.Find("btn" + bottomId, true).FirstOrDefault() as Button;
  if(bottom != null)
    ApplyColor(bottom);

  int leftId = id - 1; // left
  var left = this.Controls.Find("btn" + leftId, true).FirstOrDefault() as Button;
  if(left != null)
    ApplyColor(left);

  int rightId = id + 1; // right
  var right = this.Controls.Find("btn" + rightId , true).FirstOrDefault() as Button;
  if(right!= null)
    ApplyColor(right);
}

private void ApplyColor(Button button){
  if(button.BackColor == Color.Red)
    button.BackColor = Color.Green;
  else if(button.BackColor == Color.Green)
    button.BackColor = Red;
}

private int GetIntegerId(Button button){
  int id = 0;
  int.TryParse(button.ID.Replace("btn",""), out id);
  return id;
}


这篇关于单击按钮时更改多个按钮的颜色该按钮加上相同行和列中相邻的按钮应该更改..如果按钮为红色则变为绿色&如果它是绿色的......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:42