Click here to Skip to main content
11,706,142 members (59,775 online)
Rate this: bad
Please Sign up or sign in to vote.

I have a User Control which contains two Labels. I am using multiple user controls on a form. On click event of user control I have to determine the user control which was clicked. When user clicks outside the label area I can cast the user control from the sender object like:

UserControl1 obj=(UserControl1)sender;

But when user clicks on labels, the label is returned instead of the UserControl.
I want to get UserControl even when the label is clicked.
Posted 25-Sep-12 0:22am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I don't have a much clearer solution, but I think this is worth trying. I think you have to customize your own Label class, in my code I call it MyLabel then create a new Click event (with new keyword), then... Here is the detail.

Then if you click the label with some method was subscribed with MyLabel.Click, the sender will be your UserControl (in my code it is UserControl1).
public UserControl1()
            MyLabel l = new MyLabel();
            l.Text = "Mylabel";
            l.BorderStyle = BorderStyle.FixedSingle;//to help you see its region
            l.Size = new Size(100, 50);
            l.Click += (sender, e) =>
                //This is to test the success
                UserControl u = (UserControl)sender;
        public class MyLabel : Label
            public MyLabel()
                base.Click += (sender, e) =>
                    if(Click != null)
                        Click(this.Parent, e);
            public new event EventHandler Click;            

Hope it helps, If you are still confused, I'm glad to explain more.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

inside label's click event
UserControl1 obj=(UserControl1)yourLable.parent;
In short .Parent is a property you are searching for,
Control.parent returns it's parent control

cont2 is placed inside con1
so, Cont2.parent will return cont1
Happy Coding!
Smile | :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 151
1 OriginalGriff 151
2 Mika Wendelius 70
3 ppolymorphe 61
4 chainerlt 60
0 OriginalGriff 9,050
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,636

Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100