2

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?

Community
  • 1
  • 1
Odys
  • 8,951
  • 10
  • 69
  • 111
  • Shouldn't you just use the `Enter` and `Leave` events of the UserControl to just invalidate the panel inside it? – LarsTech Apr 30 '12 at 16:24
  • If I click the panel, the UserControl's Enter event does not fire. That's my problem – Odys Apr 30 '12 at 16:34
  • 2
    possible duplicate of [Panel not getting focus](http://stackoverflow.com/questions/3562235/panel-not-getting-focus) – Hans Passant Apr 30 '12 at 17:18

2 Answers2

2

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();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Now what about when the control gains focus by some other means, like through using the Tab key? – dynamichael Nov 25 '18 at 23:12
  • @dynamichael A UserControl has a standard Enter event for that. Unless that UserControl has a focusable control like a TextBox in it. – LarsTech Nov 26 '18 at 00:26
0

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