Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating dynamic buttons with condition,in my app product categories are dynamic they can increase,according to that product categories how many are there that much buttons have to create ,(based on that product categories buttons have to create).they are creating and event firing fine but,problem is i am applying background color to clicked button but,it doesnt remove afterclicking one to another button,still continues with other buttons color how can i solve it?
could you please help me...Thank you in adv!!!!
Posted
Updated 1-Nov-11 1:43am
v2

when the click event executes, inside that event.

you will get the object 'sender' as button instance... remove the bccolor from that sender button.

mark as answer if solves your problem
 
Share this answer
 
v2
I think what you want is that when you set the background of one button, you want to unset the background of the previously highlighted one. Is that correct?

If so, it's simple, but you need to store the previously highlighted one somewhere, otherwise you have no way to reference it!

Control highlightedControl;
Color previousBackground;

void buttonClickHandler(object sender, EventArgs ea){
 Control c = sender as Control;
 if(c != highlightedControl){
  if(null != highlightedControl) highlightedControl.BackColor = previousBackground;
  previousBackground = c.BackColor;
  highlightedControl = c;
  c.BackColor = Color.Red; // or whatever your highlight colour is
 }
 // ... (other things you want to do on button click)
}


(Edit: this text here only to make the brace appear in the right place)
 
Share this answer
 
v2
Comments
hzawary 1-Nov-11 8:23am    
Excuse Me, You can combine the if commands instead nested ifs too:)
if(c != highlightedControl && highlightedControl != null)
BobJanova 1-Nov-11 10:48am    
Except that the second if isn't for the whole block, just one statement.
hzawary 1-Nov-11 13:50pm    
Oh! So sorry, I didn't look carefully:-|

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