Click here to Skip to main content
15,887,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,
i have a question. I want to pass value from user control to window form. I have done it with like this
Coding on Form
C#
public string ID2
        {
            set
        {
            textBox1.Text = value;

        }

C#
private void button1_Click(object sender, EventArgs e)
        {

           Sample samp = new Sample();
           samp.ID = ID;
            Controls.Add(samp);
            samp.BringToFront();
        }


On USER Control
C#
public string ID2
        {
            get
            {
                return textBox1.Text;

            }
        }


C#
private void button1_Click(object sender, EventArgs e)
        {
            var textBoxContent = this.textBox1.Text;
           // ParentForm parent = (ParentForm)this.Owner;
            var parent = this.Parent as Form1;
            //var parent=this.Parent as
            parent.ID2 = ID2;
            this.Hide();

        }


This is doing very nicely. But the problem is, i am giving reference of form1 in user control. So, i can use it only on Form1 but same user control i want to use on form2. how can i do that?
plz reply fast


Thanks
Sunil Dhanerwal
Posted
Updated 15-May-18 23:51pm

Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] - it talks about forms, but it's exactly the same for user controls: the form is the Parent, the UserControl is the child.
 
Share this answer
 
i have solved it myself.
thanks for reply guys
 
Share this answer
 
Comments
Melger Stander 12-Jun-22 11:58am    
Hi How did you solve this? I'm trying the same think and it driving me mad. Just not getting it right. Ive tried your code above but still no luck. Please help a newbie out.

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