我看了所有的地方,但似乎我所看到的例子,只允許數字0-9只有數字的文本框
我正在寫一個勾股定理程序。我希望手機(Windows Phone 7)檢查文本框中是否有ANY alpha(A-Z,a-z),符號(@,%)或其他任何數字。如果不是,那麼它將繼續計算。我想檢查,所以不會有未來的錯誤。
這基本上就是我想要它做的不好的僞
txtOne - >任何字母嗎? - 不 - >任何符號 - 無 - >繼續...
我其實更喜歡一個命令來檢查字符串是否完全是的一個數字。
在此先感謝!
我看了所有的地方,但似乎我所看到的例子,只允許數字0-9只有數字的文本框
我正在寫一個勾股定理程序。我希望手機(Windows Phone 7)檢查文本框中是否有ANY alpha(A-Z,a-z),符號(@,%)或其他任何數字。如果不是,那麼它將繼續計算。我想檢查,所以不會有未來的錯誤。
這基本上就是我想要它做的不好的僞
txtOne - >任何字母嗎? - 不 - >任何符號 - 無 - >繼續...
我其實更喜歡一個命令來檢查字符串是否完全是的一個數字。
在此先感謝!
有幾種方法可以做到這一點:
您可以使用TryParse()
,並檢查返回值是不假。
您可以使用Regex
驗證:
Match match = Regex.Match(textBox.Text, @"^\d+$");
if (match.Success) { ... }
// or
if (Regex.IsMatch(textBox.Text, @"^\d+$")) { ... }
您可以使用TryParse並查看是否有結果。
見http://msdn.microsoft.com/en-us/library/system.int64.tryparse.aspx
Int64 output;
if (!Int64.TryParse(input, out output)
{
ShowErrorMessage();
return
}
Continue..
確保文本框是數字的更好方法是處理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;
}
通常情況下,您還必須處理剪貼板中的粘貼操作,但我不知道這是Windows Phone 7上的問題。 – arx 2011-12-24 21:50:48
@arx您確實很好。您還需要處理TextChanged。 – 2011-12-25 02:25:11
/// <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
}
您可以定義文本框的輸入範圍。
例子:
<TextBox InputScope="Digits"></TextBox>
這是更好地檢查所有的字符串中的字符是數字,而不是他們是否是非數字無數之一。 – Marlon 2011-12-24 21:38:41
如何使用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