1

私はあちこちを見てきましたが、私が見た例では0から9までの数字しか許されていないようです

私はピタゴラス定理プログラムを書いています。電話(Windows Phone 7)に、テキストボックスにアルファ(AZ、az)、記号(@、%)、または数字以外のものがあるかどうかを確認してもらいたいです。そうでない場合は、計算を続行します。今後エラーが発生しないように確認したい。

これは基本的に私がやりたいことの悪い擬似コードです

txtOne->任意のアルファ?-いいえ->任意の記号-いいえ->続行...

文字列が完全に数字であるかどうかを確認するコマンドを実際に使用したいと思います。

前もって感謝します!

4

6 に答える 6

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;
}

これにより、数字のみを入力できるため、テキストボックスのテキストが数字になります。


これは、10 進数値 (および明らかにバックスペース キー) を許可するために思いついたものです。

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;
} 
于 2011-12-24T21:46:44.480 に答える
4

これを行うにはいくつかの方法があります。

  1. TryParse()戻り値が false でないかどうかを使用して確認できます。

  2. Regex以下を検証するために使用できます。

    Match match = Regex.Match(textBox.Text, @"^\d+$");
    if (match.Success) { ... }
    // or
    if (Regex.IsMatch(textBox.Text, @"^\d+$")) { ... }
    
于 2011-12-24T21:16:57.763 に答える
2

または、数字キーボードのみを提供することもできます。使用できるキーボード レイアウトはいくつかあります。 リンク

さらに詳しく知りたい場合は、KeyDown イベントと KeyUp イベントを使用して、入力内容を確認し、キー押下を処理しました。 リンク

于 2011-12-24T21:34:34.203 に答える
1

テキストボックスの入力範囲を定義できます。

例:

<TextBox InputScope="Digits"></TextBox>
于 2013-05-08T12:55:30.340 に答える
0

TryParse を使用して、結果があるかどうかを確認できます。

http://msdn.microsoft.com/en-us/library/system.int64.tryparse.aspxを参照してください。

Int64 output;
if (!Int64.TryParse(input, out output)
{
    ShowErrorMessage();
    return
}
Continue..
于 2011-12-24T21:13:06.963 に答える
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
}
于 2012-11-24T01:45:56.843 に答える