2017-02-17 40 views
-1

我有一個Windows窗體應用程序。根據用戶的年齡(輸入),我想根據他們輸入的年齡來突出顯示以下標籤中的一個:「孩子,青春期,青少年,成年人」。我目前有一個年齡段文本框,用於將用戶的年齡提交到表單下方的標籤。如何根據輸入將標籤的前景色和背景色更改爲文本框

這裏是我在用: txtAge lblChild(< 12) lblPreTeen(13至15) lblTeen(16〜18) lblAdult(18>) btnSubmit按鈕

感謝。我是編碼新手,仍然掌握基礎知識。

回答

2

如果可能,我建議將文本框更改爲NumericUpDown(稱爲numAge)。在窗體編輯器中轉到NumericUpDown的屬性,然後單擊事件按鈕(閃電)。如果您雙擊ValueChanged選項,它會創建存根下面的方法:

private void numAge_ValueChanged(object sender, EventArgs e) 
    { 
     if (numAge.Value > 0 && numAge.Value < 13) 
     { 
      // Child 
      // Highlight label 
     } 
     else if (numAge.Value > 12 && numAge.Value < 16) 
     { 
      // Pre-Teen 
      // Highlight label 
     } 
     else if (numAge.Value > 15 && numAge.Value < 19) 
     { 
      // Teen 
      // Highlight label 
     } 
     else if (numAge.Value > 18) 
     { 
      // Adult 
      // Highlight label 
     } 
     else 
     { 
      // Clear the highlights 
     } 
    } 

如果你必須使用一個TextBox,使用TextChanged方法。這樣你就不需要提交按鈕:

private void txtAge_TextChanged(object sender, EventArgs e) 
    { 
     int txtAgeValue = 0; 
     if (!string.IsNullOrWhiteSpace(txtAge.Text)) 
     { 
      txtAgeValue = int.Parse(txtAge.Text); 
     } 
     if (txtAgeValue > 0 && txtAgeValue < 13) 
     { 
      // Child 
      // Highlight label 
     } 
     else if (txtAgeValue > 12 && txtAgeValue < 16) 
     { 
      // Pre-Teen 
      // Highlight label 
     } 
     else if (txtAgeValue > 15 && txtAgeValue < 19) 
     { 
      // Teen 
      // Highlight label 
     } 
     else if (numAge.Value > 18) 
     { 
      // Adult 
      // Highlight label 
     } 
     else 
     { 
      // Clear the highlights 
     } 
    } 
0

在文本框中輸入事件,您可以使用幾條if語句更新相關標籤顏色。

相關問題