-
Dec 10th, 2008, 05:08 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Limit TextBox's decimal places
Hi all,
I have some textboxes in my program. How to limit the textbox to the decimal places? For example, in the amount textbox, i would limit the user to input two decimal places only while the exchange rate textbox would limit the user to input 7 decimal places.
Thank you
-
Dec 10th, 2008, 08:02 AM
#2
Re: Limit TextBox's decimal places
This is from MartinLiss, have a look!
-
Dec 10th, 2008, 09:49 AM
#3
Re: Limit TextBox's decimal places
-
Dec 10th, 2008, 11:00 AM
#4
Thread Starter
Addicted Member
Re: Limit TextBox's decimal places
thank for your solution.
Can I just add some codes to the existing textbox instead of using the above two solutions? because the solutions above require the use of external library.
What I actually want is just adding some codes in my VB program to achieve this requirement.
Anyway, Many Thanks for your help
-
Dec 10th, 2008, 12:05 PM
#5
Re: Limit TextBox's decimal places
Try something like this for your KeyPress event (in the statement "If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then", substitute whatever limit you want for the "2").
Code:
Private Sub txtRate_KeyPress(KeyAscii As Integer)
If KeyAscii < 32 Then
' let it go, it is a control char like backspace
Exit Sub
End If
If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
If InStr(txtRate.Text, ".") > 0 Then
If Chr$(KeyAscii) = "." Then
' do not allow more than one decimal point
KeyAscii = 0
Else
If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then
' do not allow more than 2 digits past decimal point
KeyAscii = 0
End If
End If
End If
Else
KeyAscii = 0
End If
End Sub
"It's cold gin time again ..."
Check out my website here.
-
Dec 10th, 2008, 12:08 PM
#6
Re: Limit TextBox's decimal places
In some regions (just like here in hungary) the Dots are represent group of numbers, but not decimal places.
1.511.211,30
-
Dec 10th, 2008, 12:21 PM
#7
Re: Limit TextBox's decimal places
This is i wrote a sec ago, will limit decimal places, also it support the region settings (will display , or . its can be aset on the regional settings in windows)
vb Code:
Private Sub Text1_Change() Static Before As String, sDot As String Const DecimalPlaces = 3 Dim SStart As Long If InStr(Format("1,1", "#.0"), ".") > 0 Then sDot = "," Else sDot = "." If InStr(Text1.Text, sDot) > 0 Then Text1.Text = Before If Len(Text1.Text) < DecimalPlaces Then Exit Sub Text1.SelStart = Len(Text1.Text) - DecimalPlaces Exit Sub End If SStart = Text1.SelStart Text1.Text = Format(Text1.Text, "###0." + String(DecimalPlaces, "0")) If SStart > Len(Text1.Text) Then SStart = Len(Text1.Text) Text1.SelStart = SStart Before = Text1.Text End Sub
Its quite unfinished But you can start here.
Last edited by Jim Davis; Dec 10th, 2008 at 12:24 PM.
-
Dec 10th, 2008, 08:47 PM
#8
Thread Starter
Addicted Member
Re: Limit TextBox's decimal places
Originally Posted by BruceG
Try something like this for your KeyPress event (in the statement "If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then", substitute whatever limit you want for the "2").
Code:
Private Sub txtRate_KeyPress(KeyAscii As Integer)
If KeyAscii < 32 Then
' let it go, it is a control char like backspace
Exit Sub
End If
If InStr("0123456789.", Chr$(KeyAscii)) > 0 Then
If InStr(txtRate.Text, ".") > 0 Then
If Chr$(KeyAscii) = "." Then
' do not allow more than one decimal point
KeyAscii = 0
Else
If Len(Mid$(txtRate.Text, InStr(txtRate.Text, ".") + 1)) >= 2 Then
' do not allow more than 2 digits past decimal point
KeyAscii = 0
End If
End If
End If
Else
KeyAscii = 0
End If
End Sub
god, you have given me a 100 marks solution
just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.
for example, if a user has entered 100.12
and he want to change to 101.12
the sub rountine makes him cannot do that
-
Dec 10th, 2008, 09:13 PM
#9
Re: Limit TextBox's decimal places
"just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.
for example, if a user has entered 100.12 and he want to change to 101.12
The sub rountine makes him cannot do that."
-------------------
That means you have to write a little code to fix that. BruceG has given you the building blocks. Now, add a little straw and mortar.
-
Dec 10th, 2008, 09:43 PM
#10
Thread Starter
Addicted Member
Re: Limit TextBox's decimal places
Originally Posted by Code Doc
"just a little comment: In case the user has already entered 2 decimal places and then he want to change the value, the rountine that you wrote will quit his input.
for example, if a user has entered 100.12 and he want to change to 101.12
The sub rountine makes him cannot do that."
-------------------
That means you have to write a little code to fix that. BruceG has given you the building blocks. Now, add a little straw and mortar.
yeah, just add this code:
If myTxtEdit.SelStart < InStr(1, myTxtEdit.Text, ".") Then
Exit Sub
End If
-
Dec 10th, 2008, 09:44 PM
#11
Thread Starter
Addicted Member
Re: Limit TextBox's decimal places
Originally Posted by Jim Davis
This is i wrote a sec ago, will limit decimal places, also it support the region settings (will display , or . its can be aset on the regional settings in windows)
vb Code:
Private Sub Text1_Change()
Static Before As String, sDot As String
Const DecimalPlaces = 3
Dim SStart As Long
If InStr(Format("1,1", "#.0"), ".") > 0 Then sDot = "," Else sDot = "."
If InStr(Text1.Text, sDot) > 0 Then
Text1.Text = Before
If Len(Text1.Text) < DecimalPlaces Then Exit Sub
Text1.SelStart = Len(Text1.Text) - DecimalPlaces
Exit Sub
End If
SStart = Text1.SelStart
Text1.Text = Format(Text1.Text, "###0." + String(DecimalPlaces, "0"))
If SStart > Len(Text1.Text) Then SStart = Len(Text1.Text)
Text1.SelStart = SStart
Before = Text1.Text
End Sub
Its quite unfinished But you can start here.
thank you, i will debug it also. really thank you a lot
-
Dec 11th, 2008, 12:50 AM
#12
Re: Limit TextBox's decimal places
You don't have to bother with your own code. Just use my NumberBox ActiveX control. It allows you to do all that and more.
-
Dec 11th, 2008, 09:09 AM
#13
Re: Limit TextBox's decimal places
Hey lok1234, glad I was able to help. I do see now the little limit my code had, and I am glad you were able to solve it.
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|