Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I actually tried different permutations that I commented them out.
You will get them quickly.
But none worked.
What I want to accomplish here:
I have a label1 that I click inside Form1. It's click event unhide a control. When I click a label2 from inside this control, it is hiding the control after the click. Label2 Value should transfer to original label1 in Form1. But because we are still in the same click event inside Form1, label1 is not updated after I click label2 from the control.
Usually this event from control to form1 is working fine when is to change some known control. But here Im dealing with a bunch of controls, using a single event, that is dictating which label is clicked by the "sender". Clear as mud? Haha.
Thank you ;)

What I have tried:

//in control
    public partial class ColorSet : UserControl
    {
        public ColorSet()
        {
            InitializeComponent();
        }

        public delegate void ButtonWasPressed_delegate(); 	//<<<<< 1
        public event ButtonWasPressed_delegate Pressed;	  	//<<<<< 2

        //Label sel43 = new Label();
        public void Label_in_MouseClick(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.Label click = (System.Windows.Forms.Label)sender;
           // sel43.BackColor = click.BackColor;
            if (Pressed != null) Pressed(); 				//<<<<< 3
            this.Visible = false;
        }
        //public Label Select()//instead of "public void Select()"
        //{
        //    return sel43;
        //}
    }

and then
//in Form1
        public Form1()
        {
            InitializeComponent();
            colorSet1.Pressed += new ColorSet.ButtonWasPressed_delegate(colorSet1_Pressed);
        }
...
        private void Label_in_MouseClick(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.Label click = (System.Windows.Forms.Label)sender;
            colorSet1.Visible = true;
            colorSet1.Location = new Point(in_A.Location.X+15, in_A.Location.Y);
           //click.BackColor = colorSet1.Select().BackColor;
            labeX = click; 
            string debug = labeX.Name;
           
        }
        Label labeX = new Label();
        void colorSet1_Pressed()
        {
            Control ctn = this.Controls[labeX.Name];
            ctn.BackColor = labeX.BackColor;
        }
Posted
Updated 22-Jun-22 22:09pm

You are going about this a very roundabout way: if you want the UserControl to affect items in it's containing form, then the right way to do it is to have it raise an event which the form handles, uses the sender properties to get the info it needs, and then deals with the actual control on the form.

So you click the label, the click handler raises an event HideMePlease, the form handles that and does the work.

See here: Transferring information between two forms, Part 2: Child to Parent[^] - it's based around forms, but the code is exactly the same for a Control (because a Form is derived from Control).

And the more modern way to handle the sender would be:
C#
private void Label_in_MouseClick(object sender, MouseEventArgs e)
    {
    if (sender is Label labelClicked)
        {
        ... use lableClicked here ...
        }    
    }
 
Share this answer
 
Comments
_Q12_ 23-Jun-22 4:09am    
Thank you, I will try your way now
_Q12_ 23-Jun-22 4:20am    
Hmmm... i dont think is correct. Because you are still in Label_in_MouseClick event. That event is finished after I click on label1 in Form1. After I click on label2 inside the control, "Label_in_MouseClick" event from Form1 is long gone and closed. So we need to split it as I did it. I still wish a more elegant solution than what I find already... the subject remains open until then.
Thanks.
_Q12_ 23-Jun-22 4:37am    
or not ...
I find already the solution but is a bit messy... but the good news it is working.
Here is what I did:
//in control
    public partial class ColorSet : UserControl
    {
        public ColorSet()
        {
            InitializeComponent();
        }

        public delegate void ButtonWasPressed_delegate(); 	//<<<<< 1
        public event ButtonWasPressed_delegate Pressed;	  	//<<<<< 2

        Label sel43 = new Label();
        public void Label_in_MouseClick(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.Label click = (System.Windows.Forms.Label)sender;
            sel43.BackColor = click.BackColor;
            if (Pressed != null) Pressed(); 				//<<<<< 3
            this.Visible = false;
        }
        public Label Seelect()//instead of "public void Select()"
        {
            return sel43;
        }

and then
//in Form1
        public Form1()
        {
            InitializeComponent();
            colorSet1.Pressed += new ColorSet.ButtonWasPressed_delegate(colorSet1_Pressed);
        }
...
        private void Label_in_MouseClick(object sender, MouseEventArgs e)
        {
            System.Windows.Forms.Label click = (System.Windows.Forms.Label)sender;
            colorSet1.Visible = true;
            colorSet1.Location = new Point(in_A.Location.X+15, in_A.Location.Y);
            labeX = click;
            string debug = labeX.Name;
        }
        Label labeX = new Label();
        void colorSet1_Pressed()
        {
            labeX.BackColor = colorSet1.Seelect().BackColor;
        }
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900