简体   繁体   English

Usercontrol为其他usercontrol设置TextBox文本

[英]Usercontrol set TextBox text for other usercontrol

I have two diffrent usercontrol classes. 我有两个不同的usercontrol类。 I'm trying to set the textbox text for one usercontrol by another usercontrol. 我正在尝试通过另一个用户控件为一个用户控件设置文本框文本。 The get of my property is working but the set doesn't do anything. 我的属性获取正常,但集合什么也没做。 How to solve this? 如何解决呢? I've posted the related code snippets below. 我已经在下面发布了相关的代码片段。

incidentCategorySearchControl.cs incidentCategorySearchControl.cs

     public partial class incidentCategorySearchControl : UserControl
     {

     private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
      {


       incidentCategoryChange incCatChange = new incidentCategoryChange();
       //textBox1.Text = incCatChange.TextBoxCategory; // works
       incCatChange.TextBoxCategory="test"; // doesn't work

      }
    }

incidentCategoryChange.cs incidentCategoryChange.cs

    public partial class incidentCategoryChange : UserControl
    {
    public incidentCategoryChange()
    {
        InitializeComponent();
    }

    public string TextBoxCategory
    {
        get { return incidentCategoryTextBox.Text; }
        set { incidentCategoryTextBox.Text = value; }
    }

}

The value you get is default value, because just a line before you have constructed incidentCategoryChange . 您获得的值是默认值,因为在构造 incidentCategoryChange之前仅一行。 So both getter and setter aren't working. 因此,getter和setter均不起作用。

To communicate between user controls one possibility would be to provide somehow an instance of one, which TextBox (or any other property) you want to get/set, to another. 在用户控件之间进行通信的一种可能性是,以某种方式将您要获取/设置的哪个TextBox (或任何其他属性)的实例提供给另一个。

This can be done by having instance saved somewhere, to example, by using static property of same class (this require that only one instance of that user control is exists, but it's very simple to demonstrate idea): 这可以通过将实例保存在某个地方来完成,例如,通过使用同一类的static属性来完成(这要求仅存在该用户控件的一个实例 ,但是演示想法非常简单):

public partial class incidentCategoryChange : UserControl
{
    public static incidentCategoryChange Instance {get; private set;}

    public incidentCategoryChange()
    {
        InitializeComponent();
        Instance = this;
    }

    public string TextBoxCategory
    {
        get { return incidentCategoryTextBox.Text; }
        set { incidentCategoryTextBox.Text = value; }
    }
}

Now you can do 现在你可以做

incidentCategory.Instance.TextBoxCategory = "test";

Another solution would be to use events (see this question). 另一个解决方案是使用事件(请参阅问题)。 incidentCategoryChange will subscribe to event CategoryValueChanged(string) of other user control and in event handler can change the value of TextBox . incidentCategoryChange将预订其他用户控件的事件CategoryValueChanged(string) ,并且在事件处理程序中可以更改TextBox的值。

Have you tried setting incCatChange.TextBoxCategory="test"; 您是否尝试过设置incCatChange.TextBoxCategory="test"; to incCatChange.TextBoxCategory.Text="test"; incCatChange.TextBoxCategory.Text="test";

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

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