2011-12-24 83 views
1

我看了所有的地方,但似乎我所看到的例子,只允許數字0-9只有數字的文本框

我正在寫一個勾股定理程序。我希望手機(Windows Phone 7)檢查文本框中是否有ANY alpha(A-Z,a-z),符號(@,%)或其他任何數字。如果不是,那麼它將繼續計算。我想檢查,所以不會有未來的錯誤。

這基本上就是我想要它做的不好的僞

txtOne - >任何字母嗎? - 不 - >任何符號 - 無 - >繼續...

我其實更喜歡一個命令來檢查字符串是否完全是的一個數字。

在此先感謝!

+0

這是更好地檢查所有的字符串中的字符是數字,而不是他們是否是非數字無數之一。 – Marlon 2011-12-24 21:38:41

+0

如何使用TextBox數據上的TryParse方法之一。 [Int32.TryParse](http://msdn.microsoft.com/en-us/library/f02979c7%28v=VS.95%29.aspx)或[Decimal.TryParse](http://msdn.microsoft.com /en-us/library/9zbda557.aspx)取決於你的數據類型。 – 2011-12-24 21:11:02

回答

4

有幾種方法可以做到這一點:

  1. 您可以使用TryParse(),並檢查返回值是不假。

  2. 您可以使用Regex驗證:

    Match match = Regex.Match(textBox.Text, @"^\d+$"); 
    if (match.Success) { ... } 
    // or 
    if (Regex.IsMatch(textBox.Text, @"^\d+$")) { ... } 
    
2

或者你可以簡單地只給他們的數字鍵盤。有幾種不同的鍵盤佈局可以使用。 Link

如果你想做更多的深入,我已經使用KeyDown和KeyUp事件來檢查輸入和處理按鍵。 Link

+0

不處理複製粘貼 – 2011-12-25 02:25:39

+0

設備也可能有一個物理鍵盤。 – 2012-06-07 15:43:02

9

確保文本框是數字的更好方法是處理KeyPress事件。然後你可以選擇你想要允許的字符。在下面的例子中,我們不允許不是數字的所有字符:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    // If the character is not a digit, don't let it show up in the textbox. 
    if (!char.IsDigit(e.KeyChar)) 
     e.Handled = true; 
} 

這可以確保你的文本框的文本是一個數字,因爲它僅允許輸入數字。


這是我剛剛想出了允許十進制值(顯然退格鍵):

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (char.IsDigit(e.KeyChar)) 
    { 
     return; 
    } 
    if (e.KeyChar == (char)Keys.Back) 
    { 
     return; 
    } 
    if (e.KeyChar == '.' && !textBox1.Text.Contains('.')) 
    { 
     return; 
    } 
    e.Handled = true; 
} 
+2

通常情況下,您還必須處理剪貼板中的粘貼操作,但我不知道這是Windows Phone 7上的問題。 – arx 2011-12-24 21:50:48

+1

@arx您確實很好。您還需要處理TextChanged。 – 2011-12-25 02:25:11

0
/// <summary> 
/// A numeric-only textbox. 
/// </summary> 
public class NumericOnlyTextBox : TextBox 
{ 
    #region Properties 

    #region AllowDecimals 

    /// <summary> 
    /// Gets or sets a value indicating whether [allow decimals]. 
    /// </summary> 
    /// <value> 
    /// <c>true</c> if [allow decimals]; otherwise, <c>false</c>. 
    /// </value> 
    public bool AllowDecimals 
    { 
     get { return (bool)GetValue(AllowDecimalsProperty); } 
     set { SetValue(AllowDecimalsProperty, value); } 
    } 

    /// <summary> 
    /// The allow decimals property 
    /// </summary> 
    public static readonly DependencyProperty AllowDecimalsProperty = 
     DependencyProperty.Register("AllowDecimals", typeof(bool), 
     typeof(NumericOnlyTextBox), new UIPropertyMetadata(false)); 

    #endregion 

    #region MaxValue 

    /// <summary> 
    /// Gets or sets the max value. 
    /// </summary> 
    /// <value> 
    /// The max value. 
    /// </value> 
    public double? MaxValue 
    { 
     get { return (double?)GetValue(MaxValueProperty); } 
     set { SetValue(MaxValueProperty, value); } 
    } 

    /// <summary> 
    /// The max value property 
    /// </summary> 
    public static readonly DependencyProperty MaxValueProperty = 
     DependencyProperty.Register("MaxValue", typeof(double?), 
     typeof(NumericOnlyTextBox), new UIPropertyMetadata(null)); 

    #endregion 

    #region MinValue 

    /// <summary> 
    /// Gets or sets the min value. 
    /// </summary> 
    /// <value> 
    /// The min value. 
    /// </value> 
    public double? MinValue 
    { 
     get { return (double?)GetValue(MinValueProperty); } 
     set { SetValue(MinValueProperty, value); } 
    } 

    /// <summary> 
    /// The min value property 
    /// </summary> 
    public static readonly DependencyProperty MinValueProperty = 
     DependencyProperty.Register("MinValue", typeof(double?), 
     typeof(NumericOnlyTextBox), new UIPropertyMetadata(null)); 

    #endregion 

    #endregion 

    #region Contructors 

    /// <summary> 
    /// Initializes a new instance of the <see cref="NumericOnlyTextBox" /> class. 
    /// </summary> 
    public NumericOnlyTextBox() 
    { 
     this.PreviewTextInput += OnPreviewTextInput;   
    } 

    #endregion 

    #region Methods 

    /// <summary> 
    /// Numeric-Only text field. 
    /// </summary> 
    /// <param name="text">The text.</param> 
    /// <returns></returns> 
    public bool NumericOnlyCheck(string text) 
    { 
     // regex that matches disallowed text 
     var regex = (AllowDecimals) ? new Regex("[^0-9.]+") : new Regex("[^0-9]+"); 
     return !regex.IsMatch(text); 
    } 

    #endregion 

    #region Events 

    /// <summary> 
    /// Called when [preview text input]. 
    /// </summary> 
    /// <param name="sender">The sender.</param> 
    /// <param name="e">The <see cref="TextCompositionEventArgs" /> instance 
    /// containing the event data.</param> 
    /// <exception cref="System.NotImplementedException"></exception> 
    private void OnPreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     // Check number 
     if (this.NumericOnlyCheck(e.Text)) 
     { 
      // Evaluate min value 
      if (MinValue != null && Convert.ToDouble(this.Text + e.Text) < MinValue) 
      { 
       this.Text = MinValue.ToString(); 
       this.SelectionStart = this.Text.Length; 
       e.Handled = true; 
      } 

      // Evaluate max value 
      if (MaxValue != null && Convert.ToDouble(this.Text + e.Text) > MaxValue) 
      { 
       this.Text = MaxValue.ToString(); 
       this.SelectionStart = this.Text.Length; 
       e.Handled = true; 
      } 
     } 

     else 
     { 
      e.Handled = true; 
     } 
    } 

    #endregion 
} 
1

您可以定義文本框的輸入範圍。

例子:

<TextBox InputScope="Digits"></TextBox>