2014-10-08 84 views
0

我有两个不同的用户控件类。我试图通过另一个用户控件为一个用户控件设置文本框文本。我的财产取得成功,但该集合并没有做任何事情。如何解决这个问题?我已经在下面发布了相关的代码片段。Usercontrol为其他用户控件设置文本框文本

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

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

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

} 

回答

0

你有没有尝试设置incCatChange.TextBoxCategory="test";incCatChange.TextBoxCategory.Text="test";

+0

TextBoxCategory不是文本框,它是一个字符串 – 26071986 2014-10-08 14:18:16

+0

给出错误字符串不包含文本的定义 – Sybren 2014-10-08 14:19:57

1

你得到的是默认值的值,因为只是前行你有构建incidentCategoryChange。所以吸气和吸气都不起作用。

为了在用户控件之间进行通信,一种可能性是以某种方式提供一个你想获取/设置的其中一个TextBox(或任何其他属性)的实例。

这可以通过例如地方保存,示例,来完成通过使用同一类的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; } 
    } 
} 

你可以做

incidentCategory.Instance.TextBoxCategory = "test"; 

另一种解决方案是使用事件(见this问题)。 incidentCategoryChange将订阅其他用户控件的事件CategoryValueChanged(string),并且在事件处理程序中可以更改TextBox的值。

+0

我目前无法尝试此操作,但我会让你听到明天。 Thnx无论如何:) – Sybren 2014-10-08 17:22:39

+0

它工作得很好,ty! – Sybren 2014-10-09 06:19:39

相关问题