방법: 숫자 텍스트 상자 만들기

업데이트: 2007년 11월

TextBox에서 파생된 사용자 지정 컨트롤을 만들어 숫자 입력만 받아들이도록 할 수 있습니다. 이 예제는 NumericTextBox 클래스를 정의하고 이것을 폼에 배치하는 방법을 보여 줍니다.

TextBox에서 클래스를 파생시키려면

  • 프로젝트에 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;
            }
        }
    }
    

폼에 NemericTextBox 컨트롤을 추가하려면

  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. NumericTextBox에 대한 사용자 입력을 위해 InputPanel 구성 요소를 폼에 추가합니다. Smartphone 응용 프로그램의 경우 숫자 InputMode를 지정할 수 있습니다.

코드 컴파일

이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.

참고 항목

작업

방법: Smartphone 입력 모드 설정

방법: InputPanel 구성 요소 사용

개념

사용자 지정 컨트롤 개발

.NET Compact Framework 방법 항목