HOW TO:建立數字文字方塊

更新:2007 年 11 月

您可以建立衍生自 TextBox 的自訂控制項,使它只接受數值輸入。這個範例會定義 NumericTextBox 類別,並顯示如何將它放置在表單上。

若要從文字方塊衍生類別

  • 將 NumericTextBox 類別加入至專案。

    Public Class NumericTextBox
        Inherits TextBox
        Private SpaceOK As Boolean = False
    
        ' Restricts the entry of characters to digits (including hex),
        ' the negative sign, the e decimal point, and editing keystrokes (backspace).
        Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
            MyBase.OnKeyPress(e)
    
            Dim numberFormatInfo As NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
            Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
            Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
            Dim negativeSign As String = numberFormatInfo.NegativeSign
    
            Dim keyInput As String = e.KeyChar.ToString()
    
            If [Char].IsDigit(e.KeyChar) Then
                ' Digits are OK
            ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
                ' Decimal separator is OK
            ElseIf 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
                '    }
            ElseIf Me.SpaceOK AndAlso e.KeyChar = " "c Then
    
            Else
                ' Consume this invalid key and beep.
                e.Handled = True
            End If
    
        End Sub
    
    
        Public ReadOnly Property IntValue() As Integer
            Get
                Return Int32.Parse(Me.Text)
            End Get
        End Property
    
    
        Public ReadOnly Property DecimalValue() As Decimal
            Get
                Return [Decimal].Parse(Me.Text)
            End Get
        End Property
    
    
        Public Property AllowSpace() As Boolean
    
            Get
                Return Me.SpaceOK
            End Get
            Set(ByVal value As Boolean)
                Me.SpaceOK = value
            End Set
        End Property
    End Class
    
    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).
        protected override void 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
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
             keyInput.Equals(negativeSign))
            {
                // Decimal separator is OK
            }
            else if (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
            //    }
            else if (this.allowSpace && e.KeyChar == ' ')
            {
    
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }
    
        public int IntValue
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }
    
        public decimal DecimalValue
        {
            get
            {
                return Decimal.Parse(this.Text);
            }
        }
    
        public bool AllowSpace
        {
            set
            {
                this.allowSpace = value;
            }
    
            get
            {
                return this.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

編譯程式碼

這個範例需要下列命名空間的參考:

請參閱

工作

HOW TO:設定 Smartphone 輸入模式

HOW TO:使用 InputPanel 元件

概念

自訂控制項開發

.NET Compact Framework HOW TO 主題