Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to change the button color on its click and then restore it when you click on other button?

How to change the button color on its click and then restore it when you click on other button?

Scheduled Pinned Locked Moved C#
questionhelptutorial
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Swapnil 2009
    wrote on last edited by
    #1

    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

    S F 2 Replies Last reply
    0
    • S Swapnil 2009

      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

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      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.

      S 1 Reply Last reply
      0
      • S SeMartens

        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.

        S Offline
        S Offline
        Swapnil 2009
        wrote on last edited by
        #3

        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..

        1 Reply Last reply
        0
        • S Swapnil 2009

          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

          F Offline
          F Offline
          freakyit
          wrote on last edited by
          #4

          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;
          ...
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups