Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to change the buttons colors on mouse leave. I did it separately for each button but I was trying to change the colors on mouse leave and enter using only two event handlers.

What I have tried:

What I mean by that is this:

C#
private void ChangeColourOnMouseEnter(object sender, EventArgs e)
{
    this.btnNewGame.BackColor = Color.blue;
    this. btnHelp.BackColor   = Color.blue;
    this.btnExit.BackColor   = Color.blue;
}

private void ChangeColourOnMouseLeave(object sender, EventArgs e)
{
    btnNewGame.BackColor = Color.Yellow;
    btnHelp.BackColor   = Color.Yellow;
    btnExit.BackColor   = Color.Yellow;
}


As it look like, it whenever the mouse enters it will change the colour of every button. I was thinking how could it be done so that only one button will be highlighted in different color using only one method for it. (if you know what I mean)

Any help would be appreciated, thanks!.
Posted
Updated 25-Nov-17 14:49pm

If I understand correctly, you want to change the color only of the button which is hovered?
You can do it like this:
C#
private void ChangeColourOnMouseEnter(object sender, EventArgs e)
{
   ((Button)sender).BackColor = Color.blue;
}
 
private void ChangeColourOnMouseLeave(object sender, EventArgs e)
{
   ((Button)sender).BackColor = Color.Yellow;
}

The sender parameter holds a reference to the control which has raised the event.
This code first casts the sender variable (declared as object) back to its real type (Button), then call its BackColor property to assign it a different color.
Finally, select all the buttons you want to have that behaviour, and in the Properties panel, click on the Events button (at the top, just under the name and type of the control); assign the event handlers to corresponding events. The same event handler will then be able to handle the event for each of these controls.

Hope this helps.
 
Share this answer
 
Comments
fellanmorgh 25-Nov-17 21:15pm    
Awesome that worked perfectly. Its a really simple solution as well, thanks!.
phil.o 26-Nov-17 5:46am    
You're welcome :)
BillWoodruff 27-Nov-17 4:43am    
+5
phil.o 27-Nov-17 5:42am    
Thanks :)
Make your own Button control. Start a new class and inherit from Button.

Override the OnMouseEnter and OnMouseLeave events and set your button colors appropriately in those. A short sample:
C#
public class ButtonEx : Button
{
    protected override void OnMouseEnter(EventArgs e)
    {
        BackColor = Color.Yellow;
        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        BackColor = Color.LightBlue;
        base.OnMouseLeave(e);
    }
}

Of course, you can add properties to control the highlight color as appropriate.

You can either put this code in it's own Class project and add a reference to it in your existing project, or just add it as a class to your current app and compile the app. Once compiled, the control shows up in the ToolBox.
 
Share this answer
 

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