如何在c#中将文本框值从一个用户控件获取到另一个用户控件 [英] how to get textbox value from one user control to other in c#
问题描述
我的Windows窗体应用程序中有两个用户控件。在第一个用户控件中,我有一个文本框和一个保存按钮。
我在另一个用户控件中有另一个文本框。
当我点击保存 按钮然后用户控件中的文本框中的值必须显示在另一个用户控件文本框中。
我试过这样的
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.
推荐答案
这很简单,但你的方法是错误的。
看看这个:在两种形式之间传递信息,第3部分:儿童与儿童 [ ^ ]并且在任何地方它都说形式为用户控制 - 原理和技术正是如此同样。
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.
这篇关于如何在c#中将文本框值从一个用户控件获取到另一个用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!