0

I have a panel object that contains a number of controls. I want to change the backColor of that panel whenever the user clicks either on one of the controls or on the panel itself. I suspect I need to detect that the panel "has focus", but I can't figure out how.

Suggestions?

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Momerath
0

Try:

if (panel1.Focused == true)
{
panel1.BackColor = Color.Black;
}

You can also break out if statements inside of if statements.

if (panel1.Focused == true)
{
if (panel1.BackColor == Color.Blue)
{
panel1.BackColor = Color.Black;
}

else if (panel1.BackColor = Color.Black)
{
panel1.BackColor = Color.Green;
}
}

Edited by lxXTaCoXxl: n/a

0

Panels don't gain focus (they have no focus events). So what you need is to add handlers to the Enter and Leave events.

private void panel1_Enter(object sender, EventArgs e) {
    Panel p = (Panel)sender;
    p.BackColor = Color.Red;
}

private void panel1_Leave(object sender, EventArgs e) {
    Panel p = (Panel)sender;
    p.BackColor = Color.Blue;
}
0

ENTER and LEAVE events don't seem to get triggered for the panel itself. According to the documnetation ENTER occurs "when the control becomes the active control of the form". ENTER doesn't get triggered if I click on a control WITHIN the panel or when I click on the panel itself.

And, although MouseEnter and MouseLeave events DO get triggered for the panel, I really need to detect when the user actually DOES SOMETHING in the panel, not just when the mouse happens to move over it.

I'm wondering if there is some other grouping control, rather than panel, that would do what I need.

0

Enter and Leave do get triggered for a Panel, I wrote code to test it before I posted my first reply. If your control isn't gaining focus when you click on it (a button, for example) then you need to make it have focus.

Edited by Momerath: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.