PC Review


Reply
Thread Tools Rate Thread

allow decimal values in a textbox

 
 
Alb
Guest
Posts: n/a
 
      4th Aug 2003
Hi,
is possible to allow only the insertion of numerical
values in a window form textbox?
Thanks!
 
Reply With Quote
 
 
 
 
alb
Guest
Posts: n/a
 
      5th Aug 2003
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!

>
>
>.
>

 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      5th Aug 2003
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!

> >
> >
> >.
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: converting text values to number & decimal values.. John W. Vinson Microsoft Access Form Coding 2 15th Jan 2010 07:09 PM
Decimal Places in TextBox =?Utf-8?B?QU1ZIFou?= Microsoft Excel Programming 2 16th Oct 2006 03:44 PM
textbox validation (decimal) =?Utf-8?B?am9l?= Microsoft ASP .NET 1 22nd Sep 2006 10:27 AM
need converting to and formating textbox values to decimal data ty =?Utf-8?B?TmV3YmVlIEFkYW0=?= Microsoft Dot NET 6 20th Jun 2005 06:00 PM
TextBox and decimal numbers Alberto Microsoft C# .NET 3 19th Apr 2005 09:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:04 AM.