Hook Child events into Parent UserControl
-
Hi all, I was thinking there had to be an easier way to do this. I have a UserControl which contains a PictureBox and Label as children. I don't want to make the consumer have to hook the events or even expose the child controls. So how do you let the user add the event handler to the custom control and have it propagate down through to the children. I've done this which works but may give more explanation as to what I'm trying to do. Basically it's just assigning the event within the InitializeComponent().
private void SetPictureEventsToParent() { picture.MouseClick += new System.Windows.Forms.MouseEventHandler(picture_MouseClick); } void picture_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { this.OnMouseClick(e); } private void SetLabelEventsToParent() { label.MouseClick += new System.Windows.Forms.MouseEventHandler(label_MouseClick); } void label_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { this.OnMouseClick(e); }
The problem is the Label and PictureBox cover the parent and so the parent never receives the event unless I do this. Thanx