Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - TextBox Validation

I have some code that checks and makes sure that when the users enters in the field an integer from 1 - 10 has to be input.

Although if the users takes focus of the field, the "bad" data (such as "fdgfdg") is still left in the field. So could some demonstrate how when focus is lost on the field, if the data is not valid, a default value will be entered instead e.g. 5

private void textBox4_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int numberEntered = int.Parse(textBox4.Text);
                if (numberEntered < 1 || numberEntered > 10)
                {
                    MessageBox.Show("You must enter a number between 1 and 10");
                }
            }
            catch (FormatException)
            {

                MessageBox.Show("You need to enter an integer");
            }
        }
like image 844
Jay Avatar asked Jun 19 '11 20:06

Jay


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

There are several events that you can use here, Leave, LostFocus and Validating there is more discussion of these various events on MSDN here.

Under certain scenarios the Leave and the LostFocus will not fire so the best to use in your case is the Validating event:

    textBox1.Validating += new CancelEventHandler(textBox1_Validating);


    void textBox1_Validating(object sender, CancelEventArgs e)
    {
        int numberEntered;

        if (int.TryParse(textBox1.Text, out numberEntered))
        {
            if  (numberEntered < 1 || numberEntered > 10) 
            { 
                MessageBox.Show("You have to enter a number between 1 and 10");
                textBox1.Text = 5.ToString();
            }
        }
        else
        {
            MessageBox.Show("You need to enter an integer");
            textBox1.Text = 5.ToString();
        }
    }
like image 83
David Hall Avatar answered Nov 11 '22 16:11

David Hall