Field limit masking ?

  • Thread starter Thread starter John Sutor
  • Start date Start date
J

John Sutor

Here is te problem. I have a text box that I only allow numbers or decimals
to be entered (using an event delegate). The max number of characters I
allow is 4. The highest number that they should be able to enter is 1.25
and the lowest is 0.010. Currently I check the field when the leave the
control, but I need to not allow them to enter "9999" when they are typing
and not ust when they leave. Can anyone think of a way to do this?
 
John,

You might want to attach to the TextChanged event on the textbox, which
will notify you when text is changed. You should be able to make a
determination here when the text is not in a format that you want.

Hope this helps.
 
Hi John,


In such scenario I think it's better to intercept the KeyPressed (raised
before the control's value is updated) event, then you can decide if the key
is valid using the current value of the control and the new key pressed.

Pd:
I think that if you look in google by "numeric textbox" you will find a
similar solution

Cheers,
 
John

Try this two events.

private void textBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
//allow only digits and decimal
if((char.IsDigit(e.KeyChar) ==true) || (e.KeyChar == '.'))
e.Handled = false;
else
e.Handled = true;
}

private void textBox1_Leave(object sender, System.EventArgs e)
{
//handle special case validation while leaving the text box.
double number = Convert.ToDouble(textBox2.Text.Trim());
if((number < 0.010) || (number > 1.25))
MessageBox.Show("Invalid entry");
}

Shak
 
Back
Top