CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    45

    Changing color of selected Button in a group

    Hi,

    Imagine we have a group of buttons. How it is possible to change the color of a button when it is clicked and if another button of same group is clicked the last one turns back to normal and new one which is clicked changes its color?!

    Thanks.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Changing color of selected Button in a group

    The easiest way? When a button is clicked, change all of the colors of the buttons back to the default color, then change the color of the clicked button.
    ===============================
    My Blog

  3. #3
    Join Date
    Jan 2011
    Location
    Netherlands
    Posts
    45

    Re: Changing color of selected Button in a group

    It will be do able like that, but when you have lots of groups and buttons it will be pain in the ***, I am looking for a more general way.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Changing color of selected Button in a group

    Quote Originally Posted by Sean87 View Post
    It will be do able like that, but when you have lots of groups and buttons it will be pain in the ***, I am looking for a more general way.
    The "more general way" that you are looking for is called a loop. Add all of the buttons to a collection and just iterate through them.

    Code:
    void button_Click( object sender, EventArgs e )
    {
        foreach( Button b in SomeButtonCollection )
        {
            b.BackColor = SystemColors.Control
        }
    
        ((Button)sender).BackColor = SomeOtherColor;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured