简体   繁体   English

如何从一个用户控件到另一个用户控件获取文本框信息

[英]How to get Textbox information from one user control to another

I have two user controls in my windows form application. 我的Windows窗体应用程序中有两个用户控件。 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. 或任何其他最佳方法。

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 : 假定第一个控件中的文本框名为foo (因为我们看不到该代码),还假定窗体上ucSettings控件的实例名为ucSettingsInstance

in the ucSample control: ucSample控件中:

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 : 现在我们需要向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. 现在,当第一个控件中的值更改时,您可以在第二个控件中设置该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM