0
votes

I have a number of buttons on my frame and I want to show which one is selected by showing a background color. The only problem is that this background color is only visible when the mouse is hovering over the button, otherwise the button will be clear white.

Overriding the MouseEnter and MouseLeave events didn't help.

The button is inherited from the standaard Windows Forms Button and has the following method to show if it's selected:

public void SetFocus(bool focused)
{
    if (focused)
        this.BackColor = SelectColor;
    else this.BackColor = color;
}

The SelectColor is a static yellow color (to indicate the button is selected) and color is a private color stored in the class holding the color the button has when it's not selected.

Does anyone know how to show the background color, even when not hovering over the button?

1

1 Answers

0
votes

Creating a new class derived from Button:

class MyButton : Button
{
    public MyButton() : base()
    {
        this.BackColor = System.Drawing.Color.AntiqueWhite;
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        this.BackColor = System.Drawing.Color.Blue;
        base.OnEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        this.BackColor = System.Drawing.Color.AntiqueWhite;
        base.OnLeave(e);
    }
}

and then using that on the form works for me. You either have to add the button programmatically or edit the .designer.cs file.

Obviously replace my hardcoded colours with your values.