2012-08-11 54 views
0

我有我想有兩種方式來限制一個文本框:好奇的錯誤與TextChanged事件(WPF文本框)

1 - 我只希望允許數值,沒有小數

2 - 我只希望接受包含數字< = 35

我有以下的事件來處理這個問題:

private void TextBoxWorkflowCountPreviewTextInput(object sender, TextCompositionEventArgs e) 
{ 
    if (!IsNumeric(e.Text, NumberStyles.Integer)) e.Handled = true; 
} 

public bool IsNumeric(string val, NumberStyles numberStyle) 
{ 
    double result; 
    return double.TryParse(val, numberStyle, CultureInfo.CurrentCulture, out result); 
} 

private void TextBoxWorkflowCountTextChanged(object sender, TextChangedEventArgs e) 
{ 
    if (!string.IsNullOrEmpty(textBoxWorkflowCount.Text) && Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true; 
    else 
    { 
     MessageBox.Show("Must not be higher then 35"); 
     textBoxWorkflowCount.Text = "35"; 
    } 
} 

這表面上的工作完全正常 - 除了用戶將數據粘貼到文本框(不可避免)或者更好奇時使用時,如果用戶輸入一個數字然後點擊退格(使文本框再次變爲空白),則讓用戶知道其值> 35出現(儘管情況絕非如此)。如果必須的話,我可以忍受的第一個問題 - 但第二個問題是遊戲突破,並且在嘗試解決問題30分鐘後我無處可去。幫幫我!

回答

1

您的代碼失敗的首要條件,因爲

string.IsNullOrEmpty(textBoxWorkflowCount.Text) 

計算結果爲真實的,所以它的下降通過對「其他」,並顯示該消息。

if (string.IsNullOrEmpty(textBoxWorkflowCount.Text) || Convert.ToInt32(textBoxWorkflowCount.Text) <= 35) e.Handled = true; 

應該做的伎倆

1

幾個月前,我wrote a blog張貼關於行爲的文本框(的Windows Phone 7.5平臺),以及這些bahaviors之一是TextBoxInputRegexFilterBehavior,它允許你過濾輸入文本。因此,如果您熟悉行爲如何工作,你可以使用此代碼

using System.Text.RegularExpressions; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Interactivity; 

/// <summary> 
/// UI behavior for <see cref="TextBox"/> to filter input text with special RegularExpression. 
/// </summary> 
public class TextBoxInputRegexFilterBehavior : Behavior<TextBox> 
{ 
    private Regex regex; 

    private string originalText; 
    private int originalSelectionStart; 
    private int originalSelectionLength; 

    /// <summary> 
    /// Gets or sets RegularExpression. 
    /// </summary> 
    public string RegularExpression 
    { 
     get 
     { 
      return this.regex.ToString(); 
     } 

     set 
     { 
      if (string.IsNullOrEmpty(value)) 
      { 
       this.regex = null; 
      } 
      else 
      { 
       this.regex = new Regex(value); 
      } 
     } 
    } 

    /// <inheritdoc/> 
    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     this.AssociatedObject.TextInputStart += this.AssociatedObjectTextInputStart; 
     this.AssociatedObject.TextChanged += this.AssociatedObjectTextChanged; 
    } 

    /// <inheritdoc/> 
    protected override void OnDetaching() 
    { 
     base.OnDetaching(); 

     this.AssociatedObject.TextInputStart -= this.AssociatedObjectTextInputStart; 
     this.AssociatedObject.TextChanged -= this.AssociatedObjectTextChanged; 
    } 

    private void AssociatedObjectTextChanged(object sender, TextChangedEventArgs e) 
    { 
     if (this.originalText != null) 
     { 
      string text = this.originalText; 
      this.originalText = null; 
      this.AssociatedObject.Text = text; 
      this.AssociatedObject.Select(this.originalSelectionStart, this.originalSelectionLength); 
     } 
    } 

    private void AssociatedObjectTextInputStart(object sender, TextCompositionEventArgs e) 
    { 
     if (this.regex != null && e.Text != null && !(e.Text.Length == 1 && char.IsControl(e.Text[0]))) 
     { 
      if (!this.regex.IsMatch(e.Text)) 
      { 
       this.originalText = this.AssociatedObject.Text; 
       this.originalSelectionStart = this.AssociatedObject.SelectionStart; 
       this.originalSelectionLength = this.AssociatedObject.SelectionLength; 
      } 
     } 
    } 
} 

所以,用這種行爲可以編寫簡單的正則表達式表達這將過濾的用戶輸入,這樣的正則表達式=「(3 [0-5] )|([0-2]?[0-9])「

+0

哇,這是輝煌的 - 一定會用的。謝謝! – Codingo 2012-08-11 19:01:10

相關問題