Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how do i change the color of a clicked button alongside other buttons...supposing i have a form with 20 buttons (4 by 4) and want that when i click a button, the button and the one to its left, right, top and below change to another color?
Posted
Comments
[no name] 7-Sep-14 19:10pm    
Background color? Foreground color? Text foreground color? Border color? What have you tried? What was the problem with what you tried?
kbrandwijk 7-Sep-14 19:23pm    
How would 20 buttons fit inside a 4 x 4 grid...
gggustafson 8-Sep-14 1:03am    
+5
BillWoodruff 8-Sep-14 5:24am    
You can get some ideas about how to create a row-column matrix of Controls which you can easily identify by row and column position in an answer I posted recently here:

http://www.codeproject.com/Answers/816027/Hi-Everyone-My-Question-Is-About-Csharp-Windows-Fo

1 solution

Create an array of Buttons to represent your button layout, replace MyButton with your button type:
C#
MyButton[,] buttons = new MyButton[4,4];

Whenever a button is clicked, find it in your array (or store the location in the Tag property or your own custom properties, and use offsets to select the other ones, like this:
C#
int x = clickedButton.Row; // Or some other way if you don't have any custom properties in your button
int y = clickedButton.Column;

if (x > 0) MyButton[x-1,y].ForegroundColor = ...; // Or any other color
if (x < 3) MyButton[x+1,y].ForegroundColor = ...; // Or any other color
if (y > 0) MyButton[x,y-1].ForegroundColor = ...;
if (y < 3) MyButton[x,y+1].ForegroundColor = ...;
 
Share this answer
 
Comments
Member 11065163 10-Sep-14 19:34pm    
I guess it will work....going to try it. Thank u!

I had tried something else and it worked but just that the approach is kinda very long and redundant

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