Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9167683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:27:02+00:00 2026-06-17T15:27:02+00:00

I am working on a form that has lots of buttons. When the user

  • 0

I am working on a form that has lots of buttons. When the user clicks one button the background should change color. If they click another button on the form its background should change color and the previous buttons color should return back to the original color.

I can do this by hard coding in every button but this form has alot of buttons. I am sure there has to be a more efficient way of doing this

I have this so far

foreach (Control c in this.Controls)
{
    if (c is Button)
    {
        if (c.Text.Equals("Button 2"))
         {
             Btn2.BackColor = Color.GreenYellow;
         }
         else
         {

         }
    }
}

I can get the background for Btn2 to change. How would i change the background for all the other buttons in the form. Any ideas how i could do this without having to hardcode each button.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:27:04+00:00Added an answer on June 17, 2026 at 3:27 pm

    The code below will work without regard to the number of buttons on the form. Simply set the button_Click method to be the event handler of all buttons. When you click on a button, its background will change color. When you click on any other button, that button’s background will change color, and the previously-colored button’s background will revert to the default background color.

    // Stores the previously-colored button, if any
    private Button lastButton = null;
    

    …

    // The event handler for all button's who should have color-changing functionality
    private void button_Click(object sender, EventArgs e)
    {
        // Change the background color of the button that was clicked
        Button current = (Button)sender;
        current.BackColor = Color.GreenYellow;
    
        // Revert the background color of the previously-colored button, if any
        if (lastButton != null)
            lastButton.BackColor = SystemColors.Control;
    
        // Update the previously-colored button
        lastButton = current;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a long form that has several radio button groups. At the
I am working on a Magento module that has a Form where a user
I'm working on creating an HTML form that has an image in the background.
I am working with a form that has a catcha image. When a user
llo, I have a form that has two submit buttons: one accept and one
I am working on a jquery code that has a form country dropdown list
I have a user form that is working perfectly. The page makes use of
I am working on a PHP form that allows a user to add rows
I am working on win-form app that has . net 2.0 as framework. I
I am working on a classic asp form that has a number of dropdowns.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.