2014-10-08 56 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

相關問題