How can the parent retrieve child mouse events?

  • Thread starter Thread starter Leo
  • Start date Start date
L

Leo

I have a customized control inherited from panel (let's call it
panel). I use it as container and dynamically add some other
customized controls (let's call it ellipse). How can the panel
retrieve or know the mouse events on child controls (ellipses)? Any
idea are welcome. Thanks in advance.
 
Leo said:
I have a customized control inherited from panel (let's call it
panel). I use it as container and dynamically add some other
customized controls (let's call it ellipse). How can the panel
retrieve or know the mouse events on child controls (ellipses)? Any
idea are welcome. Thanks in advance.

Since your child controls derive from the Control class, they already have
public mouse events. When the child handles a mouse event (perhaps from one
of its contained controls), it can simply call OnMouseMove, for instance, to
raise the MouseMove event. The parent can then simply handle the MouseMove
event of the child:

In UserControl1:

private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
...
this.panel1.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
this.ResumeLayout(false);
}

private void panel1_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.OnMouseMove(e);
}

In Form1:

private void InitializeComponent()
{
this.userControl11 = new CSWindowsApplication1.UserControl1();
this.SuspendLayout();
//
// userControl11
//
...
this.userControl11.MouseMove += new
System.Windows.Forms.MouseEventHandler(this.userControl11_MouseMove);
this.ResumeLayout(false);
}

private void userControl11_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show(string.Format("Mouse moved to ({0}, {1})", e.X, e.Y));
}
 
Back
Top