allow decimal values in a textbox

  • Thread starter Thread starter Alb
  • Start date Start date
A

Alb

Hi,
is possible to allow only the insertion of numerical
values in a window form textbox?
Thanks!
 
I'm trying to do what you was talking about.
I'am in the KeyPress event, and I want to pass to a mwthod
the text to validate.
But I'am not able to get the current text (of my
textbox)... if I get the myTextBox.text I've got the old
value... the value that could be deleted. And e.Char is
just the new char...
I hope to have explained myself... :-)
 
Use keypress to decide if a single letter is even valid for the type of
field.
In keypress...
if e.KeyChar == ...anything but 0-9 or period ...
e.Handled = true; //Set Handled to true to cancel the KeyPress event.

You could also check if the current text already contains a period, and if
so do not allow another. Then it is all handled in the KeyPress, and
regular expressions are not needed. This is only sufficient for a simple
number containing all digits and a single period. It also does not enforce
minimum or maximum numbers, therefore does not prevent the user from
entering a value outside of the range of the property it is meant to edit.

To validate a more complex number (contains fractions, or scientific
notation) using a regular expression you should use the TextChanged or Leave
event instead.

Michael Lang, MCSD
 
Back
Top