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
"alb" <(E-Mail Removed)> wrote in message
news:0b6201c35b5f$355528a0$(E-Mail Removed)...
> 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... :-)
>
>
> >-----Original Message-----
> >You can intercept the Key events such as KeyPress. Only
> allow numeric and
> >the "." characters. You can also create a regular
> expression and verify the
> >string typed in matches that expression at each keypress,
> when the focus is
> >lost, or whenever else you deem appropriate.
> >
> >Look here for good regular expressions:
> >http://www.regexlib.com/DisplayPatterns.aspx
> >
> >Unfortunately the default TextBox control does not offer
> a validation
> >regular expression as a parameter. However, you could
> create your own
> >textbox that does.
> >
> >There is a "RegularExpressionValidator" control in the
> built in web
> >controls.
> >
> >Michael Lang, MCSD
> >
> >"Alb" <(E-Mail Removed)> wrote in message
> >news:08b901c35a97$485f1be0$(E-Mail Removed)...
> >> Hi,
> >> is possible to allow only the insertion of numerical
> >> values in a window form textbox?
> >> Thanks!
> >
> >
> >.
> >