Click here to Skip to main content
16,004,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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.
C#
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

1 solution

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.
 
Share this answer
 
Comments
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)



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