How do I make a textbox that only accepts numbers?

后端 未结 30 2315
梦如初夏
梦如初夏 2020-11-21 06:05

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I\'ve done this kind of validation by overloading the KeyPress event

30条回答
  •  佛祖请我去吃肉
    2020-11-21 06:53

    Sorry to wake the dead, but I thought someone might find this useful for future reference.

    Here is how I handle it. It handles floating point numbers, but can easily be modified for integers.

    Basically you can only press 0 - 9 and .

    You can only have one 0 before the .

    All other characters are ignored and the cursor position maintained.

        private bool _myTextBoxChanging = false;
    
        private void myTextBox_TextChanged(object sender, EventArgs e)
        {
            validateText(myTextBox);
        }
    
        private void validateText(TextBox box)
        {
            // stop multiple changes;
            if (_myTextBoxChanging)
                return;
            _myTextBoxChanging = true;
    
            string text = box.Text;
            if (text == "")
                return;
            string validText = "";
            bool hasPeriod = false;
            int pos = box.SelectionStart;
            for (int i = 0; i < text.Length; i++ )
            {
                bool badChar = false;
                char s = text[i];
                if (s == '.')
                {
                    if (hasPeriod)
                        badChar = true;
                    else
                        hasPeriod = true;
                }
                else if (s < '0' || s > '9')
                    badChar = true;
    
                if (!badChar)
                    validText += s;
                else
                {
                    if (i <= pos)
                        pos--;
                }
            }
    
            // trim starting 00s
            while (validText.Length >= 2 && validText[0] == '0')
            {
                if (validText[1] != '.')
                {
                    validText = validText.Substring(1);
                    if (pos < 2)
                        pos--;
                }
                else
                    break;
            }
    
            if (pos > validText.Length)
                pos = validText.Length;
            box.Text = validText;
            box.SelectionStart = pos;
            _myTextBoxChanging = false;
        }
    

    Here is a quickly modified int version:

        private void validateText(TextBox box)
        {
            // stop multiple changes;
            if (_myTextBoxChanging)
                return;
            _myTextBoxChanging = true;
    
            string text = box.Text;
            if (text == "")
                return;
            string validText = "";
            int pos = box.SelectionStart;
            for (int i = 0; i < text.Length; i++ )
            {
                char s = text[i];
                if (s < '0' || s > '9')
                {
                    if (i <= pos)
                        pos--;
                }
                else
                    validText += s;
            }
    
            // trim starting 00s 
            while (validText.Length >= 2 && validText.StartsWith("00")) 
            { 
                validText = validText.Substring(1); 
                if (pos < 2) 
                    pos--; 
            } 
    
            if (pos > validText.Length)
                pos = validText.Length;
            box.Text = validText;
            box.SelectionStart = pos;
            _myTextBoxChanging = false;
        }
    

提交回复
热议问题