SO WIRD'S GEMACHT: Erstellen eines numerischen Textfelds

Dieser Dokumentation für die Vorschau nur ist und in späteren Versionen geändert. Leere Themen wurden als Platzhalter eingefügt.]

Sie können erstellen ein benutzerdefiniertes Steuerelements von TextBox abgeleitet, sodass es nur eine numerische Eingabe akzeptiert. In diesem Beispiel definiert die Klasse NumericTextBox und zeigt, wie es auf dem Formular platzieren.

Dieses Beispiel überprüft das aktuelle Zeichen, wenn eine Taste gedrückt wird. Eine andere Möglichkeit zum Implementieren eines numerischen Textfelds besteht Sie Mustervergleich verwenden, um die gesamte Eingabezeichenfolge überprüfen. Weitere Informationen finden Sie unter IsMatch.

Ableiten eine Klasse von TextBox

  • Fügen Sie die NumericTextBox-Klasse hinzu Ihres Projekts.

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

So fügen Sie das Formular das NumericTextBox-Steuerelement hinzu

  1. Fügen Sie den folgenden Code hinzu Konstruktor oder Load-Ereignis des Formulars.

                                ' 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. Fügen Sie eine Komponente InputPanel hinzu Ihr Formular für Benutzereingaben in NumericTextBox. Für eine Smartphone-Anwendung können Sie eine numerische InputMode angeben.

Kompilieren des Codes

In diesem Beispiel sind Verweise auf die folgenden Namespaces erforderlich:

Siehe auch

Aufgaben

SO WIRD'S GEMACHT: Set Smartphone Input Modes

SO WIRD'S GEMACHT: Verwenden der InputPanel-Komponente

Konzepte

Benutzerdefinierte Control Development

.NET compact Framework Gewusst-wie-Themen