msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Textbox to accept numbers only (like 9.8)

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB General Discussions
Author Message
Ronald Raygun



Joined: 20 Feb 2008
Posts: 2

PostPosted: Wed Feb 27, 2008 5:35 am    Post subject: Textbox to accept numbers only (like 9.8) Reply with quote

I am using classic VB6 and have a textbox that is supposed to accept
numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc -
it would be cool if I could also "accept" numbers that have a "%"
appended like "99.9%", but this is a nice to have, not essential.

I'm wondering - is it the KeyPress event I need to trap?

Archived from group: microsoft>public>vb>general>discussion
Back to top
View user's profile Send private message
MP



Joined: 04 Oct 2007
Posts: 203

PostPosted: Tue Feb 26, 2008 11:46 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

"Ronald Raygun" wrote in message @bt.com...
>I am using classic VB6 and have a textbox that is supposed to accept
>numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc - it
>would be cool if I could also "accept" numbers that have a "%" appended
>like "99.9%", but this is a nice to have, not essential.
>
> I'm wondering - is it the KeyPress event I need to trap?

saved this snip from Rick Rothstein some time in past on this grp
you should be able to add in the % sign as well

quote
You can't do the whole process in the KeyPress event... you won't be able to
stop a user from Pasting in non-numeric text from there. Below is a solution
I've posted in the past that completely restricts TextBox entry to digits
only (whether typed or pasted).

Rick

' Put this in the form's (General)(Declarations) section
Dim LastPosition As Long

' Add the following event procedures
' =============================
Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With Text1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
Back to top
View user's profile Send private message
MikeD



Joined: 04 Oct 2007
Posts: 3348

PostPosted: Wed Feb 27, 2008 12:59 am    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

"Ronald Raygun" wrote in message @bt.com...
>I am using classic VB6 and have a textbox that is supposed to accept
>numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc - it
>would be cool if I could also "accept" numbers that have a "%" appended
>like "99.9%", but this is a nice to have, not essential.
>
> I'm wondering - is it the KeyPress event I need to trap?

No, that can lead to trouble and won't catch every way the user might enter
data (such as if the user pastes into the textbox). Best thing to do is
simply validate the data when you need to do something with it. For example,
if you're saving the data, validate it before you save it. If you're using
this number in a calculation, then validate it immediately before the
calculation. It's also not a good idea to use the LostFocus or Validate
events for this kind of thing either (even though that's what the Validate
event is intended for). Either of these events can lead to problems too.

If you really MUST restrict user input as they're entering it, it is doable.
It's just usually more hassle than it's worth and if you're showing a
message box with every "bad entry", you can easily annoy the user as he/she
is trying to correct it.

--
Mike
Microsoft MVP Visual Basic
Back to top
View user's profile Send private message
Saga



Joined: 04 Oct 2007
Posts: 809

PostPosted: Wed Feb 27, 2008 4:10 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

I use this snippet in my KeyPress event:


Private Sub Text1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 8, 48 To 57 ' BS, 0 - 9
Case 46 ' . (decimal sep)
'Bad if already there.
If InStr(Text1.Text, ".") > 0 Then
KeyAscii = 0
End If
Case Else 'Anything else
KeyAscii = 0
End Select

End Sub


Please note that this snippet is exclusively to prohibit the user from entering
a non numeric non dot character, but does not keep him/her from pasting junk
into the textbox. As others have said, you need a complement to this snippet
to validate pasted junk.

Saga

--


"Ronald Raygun" wrote in message @bt.com...
>I am using classic VB6 and have a textbox that is supposed to accept numbers only. acceptable
>numbers are (for example) 0.55, 95.8,100 etc - it would be cool if I could also "accept" numbers
>that have a "%" appended like "99.9%", but this is a nice to have, not essential.
>
> I'm wondering - is it the KeyPress event I need to trap?
Back to top
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Feb 2008
Posts: 37

PostPosted: Wed Feb 27, 2008 6:16 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

>>I am using classic VB6 and have a textbox that is supposed to accept
>>numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc - it
>>would be cool if I could also "accept" numbers that have a "%" appended
>>like "99.9%", but this is a nice to have, not essential.
>>
>> I'm wondering - is it the KeyPress event I need to trap?
>
> saved this snip from Rick Rothstein some time in past on this grp
> you should be able to add in the % sign as well

You posted my code for restricting entries to digits only; however, the
subject line for this thread indicates the OP is interested in accepting
floating point numbers. Below my signature is my standard posting for this
kind of question... it addresses both kinds of restrictions (the OP will be
interested in the one for floating point numbers).

Rick

Here are some solutions which I've posted in the past (there is code below
for both, entries with digits only and for entries with decimal points). The
routines work quite well and protects the TextBox from pasting non-numeric
entries (the user can paste valid data though) as well as stopping
non-numeric keypresses. (Be wary of KeyPress only type solutions which may
be posted later on as they can't protect against bad data being Paste'd in.)

For typing digits only in the TextBox
=====================================
Dim LastPosition As Long

Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With Text1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub


For typing floating point numbers in the TextBox
=========================================
' Set the maximum number of digits before the
' decimal point in the MaxWhole constant. Set
' the maximum number of digits after the decimal
' point in the MaxDecimal constant.
Dim LastPosition As Long

Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 4
Const MaxWhole As Integer = 2
With Text1
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub

Note that you will have check for the Text property containing a single
character consisting of a decimal point since that must be allowed as a
starting character. If you want to allow negative, as well as positive
values, then use this If statement in place of the second If statement in
the Text1_Change event code above:

If .Text Like "*[!0-9.+-]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*" & String$(MaxWhole, "#") & "[!.]*" Or _
.Text Like "?*[+-]*" Then

Note that now you will have to check the Text property for this one to see
if it contains a single plus, minus or decimal point.

I guess I should mention that I'm in the US where the decimal point is a
"dot". If your decimal point is some other characters, then make the obvious
substitutions in the If-Then tests above; or you could query the system for
the decimal point character, store it in a variable and concatenate that
into the string values above in place of the decimal point ("dot") that I
show above. In keeping with the non-APIness of this solution, here is what I
use to get the system's decimal point.

DecimalPointSymbol = Format$(0, ".")
Back to top
View user's profile Send private message
MP



Joined: 04 Oct 2007
Posts: 203

PostPosted: Wed Feb 27, 2008 5:32 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

"Rick Rothstein (MVP - VB)" wrote in
message news:%239BmmzWeIHA.6136@TK2MSFTNGP03.phx.gbl...

>
> You posted my code for restricting entries to digits only; however, the
> subject line for this thread indicates the OP is interested in accepting
> floating point numbers.

right, just happened to have that snip saved under "restricting textbox
entry" to show the basic technique
I was thinking they could adapt to their exact req's
thanks for re-posting the whole enchilada!
will expand my snippet library accordingly
Smile
mark
Back to top
View user's profile Send private message
Lorin



Joined: 04 Oct 2007
Posts: 312

PostPosted: Wed Feb 27, 2008 3:32 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

How do you do this?
Allow any programatic entry.
Allow user to move cursor through the text box (arrow keys).
Allow nothing to be pasted.
Allow nothing to be entered by the user.

I have a single line textbox that I want to display text in but the text may
be longer that the text box.
So the user can scroll horz through the text box using the arrow keys and do
nothing else.


"MP" wrote:

>
> "Ronald Raygun" wrote in message
> @bt.com...
> >I am using classic VB6 and have a textbox that is supposed to accept
> >numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc - it
> >would be cool if I could also "accept" numbers that have a "%" appended
> >like "99.9%", but this is a nice to have, not essential.
> >
> > I'm wondering - is it the KeyPress event I need to trap?
>
> saved this snip from Rick Rothstein some time in past on this grp
> you should be able to add in the % sign as well
>
> quote
> You can't do the whole process in the KeyPress event... you won't be able to
> stop a user from Pasting in non-numeric text from there. Below is a solution
> I've posted in the past that completely restricts TextBox entry to digits
> only (whether typed or pasted).
>
> Rick
>
> ' Put this in the form's (General)(Declarations) section
> Dim LastPosition As Long
>
> ' Add the following event procedures
> ' =============================
> Private Sub Text1_Change()
> Static LastText As String
> Static SecondTime As Boolean
> If Not SecondTime Then
> With Text1
> If .Text Like "*[!0-9]*" Then
> Beep
> SecondTime = True
> .Text = LastText
> .SelStart = LastPosition
> Else
> LastText = .Text
> End If
> End With
> End If
> SecondTime = False
> End Sub
>
> Private Sub Text1_MouseDown(Button As Integer, _
> Shift As Integer, X As Single, Y As Single)
> With Text1
> LastPosition = .SelStart
> 'Place any other MouseDown event code here
> End With
> End Sub
>
> Private Sub Text1_KeyPress(KeyAscii As Integer)
> With Text1
> LastPosition = .SelStart
> 'Place any other KeyPress checking code here
> End With
> End Sub
>
>
>
Back to top
View user's profile Send private message
Saga



Joined: 04 Oct 2007
Posts: 809

PostPosted: Wed Feb 27, 2008 5:39 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

A good start would be to set the Locked property of the textbox to True.

Saga
--

"Lorin" wrote in message @microsoft.com...
> How do you do this?
> Allow any programatic entry.
> Allow user to move cursor through the text box (arrow keys).
> Allow nothing to be pasted.
> Allow nothing to be entered by the user.
>
> I have a single line textbox that I want to display text in but the text may
> be longer that the text box.
> So the user can scroll horz through the text box using the arrow keys and do
> nothing else.
>
>
> "MP" wrote:
>
>>
>> "Ronald Raygun" wrote in message
>> @bt.com...
>> >I am using classic VB6 and have a textbox that is supposed to accept
>> >numbers only. acceptable numbers are (for example) 0.55, 95.8,100 etc - it
>> >would be cool if I could also "accept" numbers that have a "%" appended
>> >like "99.9%", but this is a nice to have, not essential.
>> >
>> > I'm wondering - is it the KeyPress event I need to trap?
>>
>> saved this snip from Rick Rothstein some time in past on this grp
>> you should be able to add in the % sign as well
>>
>> quote
>> You can't do the whole process in the KeyPress event... you won't be able to
>> stop a user from Pasting in non-numeric text from there. Below is a solution
>> I've posted in the past that completely restricts TextBox entry to digits
>> only (whether typed or pasted).
>>
>> Rick
>>
>> ' Put this in the form's (General)(Declarations) section
>> Dim LastPosition As Long
>>
>> ' Add the following event procedures
>> ' =============================
>> Private Sub Text1_Change()
>> Static LastText As String
>> Static SecondTime As Boolean
>> If Not SecondTime Then
>> With Text1
>> If .Text Like "*[!0-9]*" Then
>> Beep
>> SecondTime = True
>> .Text = LastText
>> .SelStart = LastPosition
>> Else
>> LastText = .Text
>> End If
>> End With
>> End If
>> SecondTime = False
>> End Sub
>>
>> Private Sub Text1_MouseDown(Button As Integer, _
>> Shift As Integer, X As Single, Y As Single)
>> With Text1
>> LastPosition = .SelStart
>> 'Place any other MouseDown event code here
>> End With
>> End Sub
>>
>> Private Sub Text1_KeyPress(KeyAscii As Integer)
>> With Text1
>> LastPosition = .SelStart
>> 'Place any other KeyPress checking code here
>> End With
>> End Sub
>>
>>
>>
Back to top
View user's profile Send private message
Mike Williams



Joined: 04 Oct 2007
Posts: 1309

PostPosted: Wed Feb 27, 2008 11:49 pm    Post subject: Re: Textbox to accept numbers only (like 9.8) Reply with quote

"Lorin" wrote in message @microsoft.com...

> How do you do this?
> Allow any programatic entry.
> Allow user to move cursor through the text box (arrow keys).
> Allow nothing to be pasted.
> Allow nothing to be entered by the user.

Text1.Locked = True

Mike

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB General Discussions All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group