Export (0) Print
Expand All

How to: Create a Numeric Text Box

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can create a custom control derived from TextBox so that it accepts only numeric input. This example defines the NumericTextBox class and shows how to place it on the form.

This example checks the current character whenever a key is pressed. Another way to implement a numeric text box is to use pattern matching to verify the entire input string. For more information, see IsMatch.

To derive a class from TextBox

  • Add the NumericTextBox class to your project.

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

To add the NumericTextBox control to the form

  1. Add the following code to the form's constructor or Load event.

    
    // 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. Add an InputPanel component to your form for user input into the NumericTextBox. For a Smartphone application, you can specify a numeric InputMode.

Show:
© 2014 Microsoft