2016-01-12 42 views
0

C#Winforms用戶控件位於此處。無法通過組合框選擇獲取Label顯示值

所以我有一個名爲酒精的標籤對象。我的組合框對象被命名爲snryeastTypeComboBox。我想稍後保留數學酒精的數學。 我試圖在標籤中顯示數字,但它不工作......任何想法?

public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e) 
{ 
    if (snryeastTypeComboBox.SelectedText == "CSM") 
    { 
     var alcoholTolerance = 14; 
     alcohol.Text = alcoholTolerance.ToString(); 
    } 
+1

你有沒有調試代碼..可以是snryeastTypeComboBox.SelectedText ==「CSM」在你的情況不是真的嗎? –

回答

1

爲了理解您的問題,我認爲您需要在從組合框中選擇一個值時觸發該事件。如果是這種情況,您應該訂閱SelectedIndexChanged事件而不是TextChanged

TextChanged如果用戶可以通過在組合框中輸入更改值,則應該使用事件。

1

嘗試,而不是執行以下操作:

public void snryeastTypeComboBox_TextChanged(object sender, EventArgs e) 
{ 
    if (snryeastTypeComboBox.Text == "CSM") 
    { 
     var alcoholTolerance = 14; 
     alcohol.Text = alcoholTolerance.ToString(); 
    } 

只。文本替換.SelectedText。

+0

我也會同意@Yogi的說法,那就是'SelectedIndexChanged'事件是一個更好的事件在這裏使用,但是上面的改變仍然是相關的。 – KMB

0

要達到此目的,您的代碼需要寫入事件SelectedIndexChanged而不是TextChanged

而不是SelectedText你應該使用Text

下面的代碼將正常工作。

private void snryeastTypeComboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     if (snryeastTypeComboBox.Text == "CSM") 
     { 
      var alcoholTolerance = 14; 
      alcohol.Text = alcoholTolerance.ToString(); 
     } 

    }