Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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!
Posted
Updated 9-Sep-14 17:58pm
v3
Comments
Agent__007 9-Sep-14 23:56pm    
Are IDs of these buttons like you mentioned, like btn1, btn10, btn25, etc..?
Member 11065163 10-Sep-14 9:07am    
yes sir!

1 solution

Assuming you are using WinForms, you can have a single handler for Click event for all buttons which may look like (NOT TESTED and POORLY CODED example but I hope it's enough to give you an idea):

C#
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;
}
 
Share this answer
 
Comments
Member 11065163 10-Sep-14 11:07am    
everything seem to be cool with d code but in d second to d last line...there is a problem with d 'ID' in { int.tryParse(button.ID.Replace("btn", ""), out id);}.....the erro message goes like dis "'System.Windows.Forms.Button' does not contain a definition for ID and no extension method ID accepting a first argument of type 'System.Windows.Forms.Button' could be found (are u missing a using directive or an assembly reference?)"
Agent__007 10-Sep-14 23:52pm    
Damned! Sorry, it should be 'Name' and not 'ID'. Let me know if that works.
Member 11065163 11-Sep-14 18:56pm    
Sorry don't mean to border u. I replaced the 'ID' with 'Name' as u instructed and now there is no error in the code...remember, On_Load the buttons appear green. So I run the code and it executes normal but when u click a button they remain green, there is no effect as regards the change of color.

I'm a novice pls bear with me!...I don't know where to call the single event handler u created or which function to call even....I tried calling the 'ChangeButtonColors' in the individual button click_Event handler but all to no avail. BELOW IS THE FULL CODE.....Thouhg i used TableLayoutPanel to display the buttons.

namespace TheMindBoggleGame
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
foreach (Button btn in Panel1.Controls) {
btn.BackColor = Color.Green;
}

}

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

//find adjacent buttons
int id = GetIntegerId(button);
int topId = id - 5;
var top = this.Controls.Find("btn" + topId, true).FirstOrDefault() as Button;
if (top != null) ApplyColor(top);

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

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

int rightId = id + 1;
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 = Color.Red;
}

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

}
}
}
Member 11065163 10-Sep-14 11:13am    
...and a result of that the code is not compilling
Agent__007 11-Sep-14 23:16pm    
Please check if you are getting the "target" buttons by "this.Controls.Find("btn" + {id}, true).FirstOrDefault() as Button" statements, by "target" I mean the intended ones and not the default Button instance. You can decide that by looking at the values of other properties that you have set in your code/designer.

If they are not the "target" ones, probably you need to change this statement and try to find the buttons in your Panel instead of entire form, it should work. But if they indeed are the "target" ones, I am afraid I can't help you further on this, you need to debug the code in order to find the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900