方法 : 数値、テキスト ボックスを作成します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

数値のみ入力が受け付けられますように TextBox から派生したカスタム コントロールを作成できます。 次の使用例 NumericTextBox クラスを定義し、フォームに配置する方法を示します。

この例は、キーが押されたときに、現在の文字をチェックします。 数値テキスト ボックスを実装する別の方法をパターン マッチングを使用して入力文字列全体を確認します。 詳細については、「IsMatch」を参照してください。

テキスト ボックス (TextBox) コントロールからクラスを派生させる

  • NumericTextBox クラスをプロジェクトに追加します。

                                Public
                                Class NumericTextBox
        Inherits TextBox
        Private SpaceOK AsBoolean = False
        ' Restricts the entry of characters to digits (including hex),    ' the negative sign, the e decimal point, and editing keystrokes (backspace).ProtectedOverridesSub OnKeyPress(ByVal e As KeyPressEventArgs)
            MyBase.OnKeyPress(e)
    
            Dim numberFormatInfo As NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
            Dim decimalSeparator AsString = numberFormatInfo.NumberDecimalSeparator
            Dim groupSeparator AsString = numberFormatInfo.NumberGroupSeparator
            Dim negativeSign AsString = numberFormatInfo.NegativeSign
    
            Dim keyInput AsString = e.KeyChar.ToString()
    
            If [Char].IsDigit(e.KeyChar) Then            ' Digits are OKElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then            ' Decimal separator is OKElseIf e.KeyChar = vbBack Then            ' Backspace key is OK            '    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)            '    {            '     // Let the edit control handle control and alt key combinations            '    }ElseIfMe.SpaceOK AndAlso e.KeyChar = " "c ThenElse            ' Consume this invalid key and beep.
                e.Handled = TrueEndIfEndSubPublicReadOnlyProperty IntValue() AsIntegerGetReturn Int32.Parse(Me.Text)
            EndGetEndPropertyPublicReadOnlyProperty DecimalValue() AsDecimalGetReturn [Decimal].Parse(Me.Text)
            EndGetEndPropertyPublicProperty AllowSpace() AsBooleanGetReturnMe.SpaceOK
            EndGetSet(ByVal value AsBoolean)
                Me.SpaceOK = value
            EndSetEndPropertyEndClass
    
                                public
                                class NumericTextBox : TextBox
    {
        bool allowSpace = false;
    
        // Restricts the entry of characters to digits (including hex), the negative sign,// the decimal point, and editing keystrokes (backspace).protectedoverridevoid OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
    
            NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;
    
            string keyInput = e.KeyChar.ToString();
    
            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }
            elseif (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
             keyInput.Equals(negativeSign))
            {
                // Decimal separator is OK
            }
            elseif (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)//    {//     // Let the edit control handle control and alt key combinations//    }elseif (this.allowSpace && e.KeyChar == ' ')
            {
    
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }
    
        publicint IntValue
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }
    
        publicdecimal DecimalValue
        {
            get
            {
                return Decimal.Parse(this.Text);
            }
        }
    
        publicbool AllowSpace
        {
            set
            {
                this.allowSpace = value;
            }
    
            get
            {
                returnthis.allowSpace;
            }
        }
    }
    

NumericTextBox コントロールをフォームに追加するには

  1. 次のコードは、フォームのコンストラクターまたは Load イベントを追加します。

                                ' Create an instance of NumericTextBox.
                                Dim NumericTextBox1 As NumericTextBox = New NumericTextBox()
    NumericTextBox1.Parent = Me
    
    ' Draw the bounds of the NumericTextBox.
    NumericTextBox1.Bounds = New Rectangle(5, 5, 150, 100)
    
                                // Create an instance of NumericTextBox.
    NumericTextBox numericTextBox1 = new NumericTextBox();
    numericTextBox1.Parent = this;
    
    //Draw the bounds of the NumericTextBox.
    numericTextBox1.Bounds = new Rectangle(5, 5, 150, 100);
    
  2. InputPanel にユーザー入力フォームに、 NumericTextBox コンポーネントを追加します。 Smartphone アプリケーションを数値の InputMode を指定できます。

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

処理手順

方法 : Smartphone の入力モードの設定

方法 : InputPanel コンポーネントを使用します。

概念

カスタム コントロールの開発

.NET コンパクトなフレームワーク方法を説明したトピックの検索