Search code examples
c#.netwinformsuser-controls

how to redirect events at a usercontrol


I've a user control, pretty simple just a label and a picturebox, they are docked that you can't even see the underlying most of the events never occurs (click, hover and others)

i think (i could be so wrong), it's because the events won't be triggered as they are being clicked either on the label or the picturebox and not on the background

i need somehow to redirect these events (i.e create that events in the usercontrol and somehow redirect them to my code and with my code i mean the form that uses them) or any other way to capture those events

i've seen like 5 topics about the same problem and none were solved, any workaround ?


Solution

  • You need to redirect your Click event of your label and pictureBox to the OnClick event of your user control using this code:

    public UserControl1()
    {
       InitializeComponent();
    
       this.label1.Click += myClickEvent;
       this.pictureBox1.Click += myClickEvent;
    }
    
    private void myClickEvent(object sender, EventArgs e)
    {
       this.OnClick(e);
    }