0

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.

2
Contributors
1
Reply
31
Views
4 Years
Discussion Span
Last Post by JOSheaIV
1

I really haven't worked with user controls myself, something I have been meaning to look into myself.

But based on what you're asking, I want to say you could use delegate? I have really started to sink my teeth into these lately with a current project related to threading, and I use them to pass back data to a function once it completes a task (in a completely different class that is not connected in anyway).

You could use it here where you click the button, and the delegate calls the function in the other user control, passing its data.

Here I through something together. I wasn't able to test it so you'll have to, but I want to say this will work (I am still somewhat new to delegates, but I feel confident this should work)

namespace project
{
    public delegate void PassData_CallTo(string textBoxData); //note where this is placed. After namespace, but before any class

    public partial class Form1 : Form
    {
        public Form1()
        {
            UserControl ucSettings = new UserControl();
            UserControl ucSample = new UserControl(new PassData_CallTo(ucSettings.textBoxChange));

            //show your UserControls here, or do whatever you want
        }
    }
}

namespace project
{
    public partial class ucSettings : UserControl
    {
        public ucSettings()
        {
            InitializeComponent();
        }

        public void textBoxChange(string input) //change the display of the textBox
        {
            txtvolumeMin.Text = input;
        }
    }
}

namespace project
{
    public partial class ucSample : UserControl
    {
        PassData_CallTo displayData_There;

        public ucSample(PassData_CallTo displayData_There)
        {
            InitializeComponent();

            this.displayData_There = displayData_There; //assign passed in delegate
        }

        private void btnSave_Click(object sender, EventArgs e) //when the button is clicked
        {   
            if(displayData_There != null) //checks to make sure not null
            {
                displayData_There(txtSamplevolume.Text); //pass the data over to the other UserControl
            }
        }
    }
}

Here what we are doing is saying "hey when you click the button, go send this data over to that function in that other UserControl". From how I understand delegates, they kind of work like pointers

Edited by JOSheaIV

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.