How to change the button color on its click and then restore it when you click on other button?
-
Hello friends, I had a question.actually I have a web page in which there are multiple buttons. Whenever I click on a button the contents get displayed in the bellow panel and the color of the button changes. Then when I click on another button its color changes but the first button color persists. protected void Button_headers_Click(object sender, EventArgs e) { this.Button_headers.BackColor = Color.Blue; } So what I want is the color of the button on which I click should change and when I click on second button the first one color should get restored. I would be very much thankful to you if you help me in this matter. Regards Swapnil
-
Hello friends, I had a question.actually I have a web page in which there are multiple buttons. Whenever I click on a button the contents get displayed in the bellow panel and the color of the button changes. Then when I click on another button its color changes but the first button color persists. protected void Button_headers_Click(object sender, EventArgs e) { this.Button_headers.BackColor = Color.Blue; } So what I want is the color of the button on which I click should change and when I click on second button the first one color should get restored. I would be very much thankful to you if you help me in this matter. Regards Swapnil
Hi, write a method to which you pass the button clicked. Call this method from each eventhandler (for a button click). The method should look like this:
private void SetButtonColor(object sender) {
this.Button_headers.BackColor = (sender.Equals(this.Button_headers) ? Color.Blue : Color.White);
this.MySecondButton.BackColor = (sender.Equals(this.MySecondButton) ? Color.Blue : Color.White);
...
}Let me know if this helps you. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
-
Hi, write a method to which you pass the button clicked. Call this method from each eventhandler (for a button click). The method should look like this:
private void SetButtonColor(object sender) {
this.Button_headers.BackColor = (sender.Equals(this.Button_headers) ? Color.Blue : Color.White);
this.MySecondButton.BackColor = (sender.Equals(this.MySecondButton) ? Color.Blue : Color.White);
...
}Let me know if this helps you. Regards Sebastian
It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.
Thanx Sebastian.. Thank you very much..It really worked well.. Really I knew it was not a bug..but Its a great feuture.. thank you very much..
-
Hello friends, I had a question.actually I have a web page in which there are multiple buttons. Whenever I click on a button the contents get displayed in the bellow panel and the color of the button changes. Then when I click on another button its color changes but the first button color persists. protected void Button_headers_Click(object sender, EventArgs e) { this.Button_headers.BackColor = Color.Blue; } So what I want is the color of the button on which I click should change and when I click on second button the first one color should get restored. I would be very much thankful to you if you help me in this matter. Regards Swapnil
hi if you don't know the count of buttons you will use write it dynamic
private object lastButton;
private void SetButtonColor(object sender) {if(lastButton != null && lastButton != sender)
{
lastButton.BackColor = Color.White;
}sender.BackColor = Color.Blue;
lastButton = sender;
...
}