SoFunction
Updated on 2025-03-01

C# textbox real-time input value detection method

C# textbox real-time input value detection

Check whether the textbox real-time input value is in English, the splitter and the numeric value (the value can be positive or negative)

private void textBoxMarker_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ( >= '0' &&  <= '9' ||  == '-' ||  == ',')
            {
                 = false;//Allow input            }
            else
            {
                 = true;//Enter not allowed                ("Please enter integer characters(like“-5”“-10”“2”“3”)!", "warn", , );
            }
        }

C# limits the range of input values ​​in TextBox control

For example:

For example, you need to restrict only 1~100 numbers in the TextBox1 control (first set the MaxLength property of TextBox1 to 3):

1. The first thing to limit input is only numerical values

It cannot be a letter or other symbol; choose to add the KeyPress event of textBox1, the code is as follows:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(()) &&  != (char)8)
                 = true;
        }

2. Restrict the input value range 1~100

Select to add TextChanged event of textBox1, the code is as follows:

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if ( == "") 
                 = (); 
            int number = ();
             = ();
            if (number <= 100)
            {
                return;
            }
             = (2);
             = ;
        }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.