0
votes

I need to change button BackColor on focus.
Now I am using MouseOverBackColor<=Silver in a button.
When I focus the button by mouse, its backcolor changed to Silver.
When I focus the button by tab key or .focus() from code behind,I want to change the backcolor of it to Silver.
Which event should I use?
Can anybody help me,please? Thanks.

2

2 Answers

1
votes

You can use the GotFocus and LostFocus, or the Enter and Leave events for this purpose.

private void myBtn_GotFocus(object sender, EventArgs e)
{
    myBtn.BackColor = Color.Silver;
}

private void myBtn_LostFocus(object sender, EventArgs e)
{
    myBtn.BackColor = SystemColors.Control;
}
1
votes

In order to keep consistency, you may have both tab and mouseover to change the button colour when focus is on the Form.

But you need to override low level Got, LostFocus events.

protected override void OnLostFocus(EventArgs e)
{
    base.OnLostFocus(e);
}

protected override void OnGotFocus(EventArgs e)
{
    base.OnGotFocus(e);
}

Reference