Click here to Skip to main content
12,945,706 members (52,297 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I hav two user control's in my windows form application. In the first user control i have one "textbox" and one "save" button.
I have another "Textbox" in another user control.
when i click "save" button then what ever the value in "textbox" in user control one has to display in another user control "Textbox".

I have tried like this
namespace project
    public partial class ucSample : UserControl
        private double transferVolume;
        public double TransferVolume
            get { return transferVolume; }
            set { transferVolume = value; }
      public ucSample()
       private void btnSave_Click(object sender, EventArgs e)
            TransferVolume = double.Parse(txtSamplevolume.Text);

In another user control i am trying like as shown below.
namespace project
    public partial class ucSettings : UserControl
        ucSample samplevolume = new ucSample();
        public ucSettings()
 private void  txtvolumeMin_TextChanged(object sender, EventArgs e)
           txtvolumeMin.Text = samplevolume.TransferVolume.ToString();

Please can any one help me what mistake i am doing here. I am using property to transfer value. I am not able figure it out what is the mistake. or any other best way to do this .

Thanks in advance.
Posted 20-Feb-13 23:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is pretty simple, but you are going about it the wrong way.
Have a look at this: Transfering information between two forms, Part 3: Child to Child[^] and everywhere it says "form" read "user control" - the principle and techniques are exactly the same.
vebi1000 21-Feb-13 6:44am
I have tried to understand your concept and even i have downloaded your code. exactly that is what i want but here i am confusing little bit about parent and child. I have understand the property and event handler in child.cs but i am confusing on child 1 and child 2. I am basic C# learner. Can you describe little bit more on
"child1.DataAvailable += new EventHandler(child_DataAvailable);" this line and "child_DataAvailable" that function.
OriginalGriff 21-Feb-13 7:14am
In your case, the two user controls are the children, and the form they are visible in is the parent - since it needs to know about the two controls in order to create the instances and add them to it's Controls list (which it does behind the scenes in the .Designer.cs file) it should provide the routing.
You can think of a user control as a small form (a Form is derived from Control so it's not that much of a stretch) and a user control can have properties, methods and events just like forms can.
So all you need to do is add the event code and a property to the user control that provides the data, and a property to the user control that accepts the data. You then handle the event in the form and read the data from one control, and hand it to the other. It's a bit like a restaurant: the kitchen tells the waiter the food is ready, he collects it and takes the food to the table. At no point does the customer see the kitchens or the kitchen staff see the customer.
vebi1000 21-Feb-13 7:29am
Thank you very much for your clear explanation , now i understand clearly.
OriginalGriff 21-Feb-13 7:38am
You're welcome!

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,180
Maciej Los 2,453
Jochen Arndt 1,910
ppolymorphe 1,785

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
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