7

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");
            }
        }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jay
  • 185
  • 2
  • 2
  • 5
  • 1
    @Gats: This is winforms desktop app... – Peter Kelly Jun 19 '11 at 20:46
  • This is winforms not a web page. –  Jun 19 '11 at 20:46
  • 1
    @Gats: Even if it was a web app, server-side validation is mandatory. Client-side validation should be a convenience addition to server-side validation. – František Žiačik Jun 19 '11 at 20:46
  • 1
    You should also take a look at an [ErrorProvider](http://msdn.microsoft.com/en-us/library/system.windows.forms.errorprovider(v=VS.100).aspx) which is a handy way of showing the user that an input is wrong. – Zeus Jun 19 '11 at 20:59
  • I am a tool. That's what happens when you answer things at 4am :) – Gats Jun 20 '11 at 02:13

4 Answers4

15

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();
        }
    }
David Hall
  • 32,624
  • 10
  • 90
  • 127
0

Have a look here and I would use the TryParse

Community
  • 1
  • 1
CheGueVerra
  • 7,849
  • 4
  • 37
  • 49
0

if you are hand-rolling validation like you do here, all you need to do is to set the default value after you MessageBox.Show()

in standard winforms I don't think you have any framework support for validation, but you could look at this: http://msdn.microsoft.com/en-us/library/ms951078.aspx for inspiration so you don't scatter this logic throughout your app

AndreasKnudsen
  • 3,453
  • 5
  • 28
  • 33
0

Use the Leave event on the textbox control to validate and set the default value

Peter Kelly
  • 14,253
  • 6
  • 54
  • 63