Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm
Hi,
 
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()
            {
            InitializeComponent();
            
            }
 
       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()
            {
            InitializeComponent();
            }
        
 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 21-Feb-13 0:15am

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
vebi1000 at 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 at 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 at 21-Feb-13 7:29am
   
Thank you very much for your clear explanation , now i understand clearly.
OriginalGriff at 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
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2014
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