Odys
Odys

Reputation: 9090

Enter event not firing in Panel inside a UserControl

I added a Panel inside a UserControl i design time.

Then, I added this control to a form.

I want to show a focus dashed border when the control has the focus.

Unfortunately, the Enter event from the panel never fires. I only get a fire when I click on the user control itself.

To extend this question. How can I forward events from controls inside a user control to the base user control? A comment from Hans Passant in this question says that by default events are forwarded to their direct parent. I didn't change any of the control's properties. What am I doing wrong? Is there an obvious property I need to change on each control i order to force it to forward unhandled events?

I am using DevExpress controls but this behavior is same in windows WinForms controls.

edit: I understand that panel might not be able to get focus. If this is true, how do I forward each mouse event to the parent control?

Upvotes: 2

Views: 3833

Answers (2)

Ozkan Konca
Ozkan Konca

Reputation: 115

Use panel1.Select() on MouseClick event of the panel and you will be able to trigger panel1.Enter and panel1.Leave

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

Based on your comment, from inside your UserControl, handle the panel's MouseDown event and set the focus to the parent control:

public UserControl1() {
  InitializeComponent();
  panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
}

void panel1_MouseDown(object sender, MouseEventArgs e) {
  if (!this.Focused)
    this.Focus();
}

Upvotes: 2

Related Questions