TerribleDog
TerribleDog

Reputation: 1247

Textbox validation when lower than the desired number

In my textbox, the value accepted should be in range of 80 - 160. There are no problems when the user inputs number greater than 160. But for the 80, when the user starts typing, it automatically changes to 80 since a 1-digit integer is lower than 80. What approach should I take here?

private void tbox_Power_TextChanged(object sender, EventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb.Text != String.Empty && int.Parse(tb.Text) > 160)
        tb.Text = "160";
    else if (tb.Text != String.Empty && int.Parse(tb.Text) < 80)
        tb.Text = "80";
    else if (tb.Text == String.Empty)
        tb.Text = "0";
}

Upvotes: 0

Views: 567

Answers (2)

Rufus L
Rufus L

Reputation: 37020

You could always use a NumericUpDown control for getting numeric input from the user. You can set the Minimum and Maximum values, and don't have to do any parsing.

Upvotes: 1

Bogdan Doicin
Bogdan Doicin

Reputation: 2416

The best solution I can think of is to allow the user to type the desired number then, after a short while (let's say, 5 seconds) to do the desired processing. You can use a Timer instance, set the delay to 5000 and set it on as a LostFocus event handler. In the Timer's Tick event you can do whatever processing you want.

Upvotes: 1

Related Questions