Reputation: 193
I have two user controls in my windows form application. In the first user control I have one "textbox" and one "save" button.
And I have "Textbox" in another user control.
when I click "save" button in the frist user control then whatever the value in the "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 to write logic 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.
Upvotes: 1
Views: 3648
Reputation: 67928
It looks like you have the code reversed. Let's assume the text box in the first control is named foo
(because we don't see that code) and let's also assume the instance of the ucSettings
control on your form is named ucSettingsInstance
:
in the ucSample
control:
public event EventHandler TextChanged;
private void foo_TextChanged(object sender, EventArgs e)
{
if (this.TextChanged != null) { this.TextChanged(sender, e); }
}
in your form consume that new event:
private void ucSettingsInstance_TextChanged(object sender, EventArgs e)
{
ucSettingsInstance.MinimumVolume = ucSampleInstance.TransferVolume;
}
now we need to add a new property to ucSettings
:
public double MinimumVolume
{
get { return minimumVolume; }
set
{
minimumVolume = value;
txtVolumnMin.Text = minimumVolume.ToString();
}
}
and now when the value is changed in the first control you can set the value in the second control.
Upvotes: 1