|
-
Feb 16th, 2007, 11:16 AM
#1
Thread Starter
Lively Member
[RESOLVED] Again with Decimal...........
hi,
i am able to validate decimal input with TextBox. I used the following codings so that only 2 numbers are allowed aftr decimal.also, only 1 decimal is allowed in a textbox.
VB Code:
Option Explicit
Dim k, s, c
Private Sub Text1_Change()
If Len(Text1.Text) = s Then
Command1.SetFocus
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
k = Chr(KeyAscii) Like "[0-9]"
If KeyAscii = 46 And c >= 1 Then
KeyAscii = 0
End If
If KeyAscii = 46 Then
s = Len(Text1.Text) + 3
c = c + 1
End If
End Sub
the problem arises when i use backspace.i am unable to type Decimal point again.
hw shud i track whether decimal point is being erased or not.so that aftr typing decimal point for second time{(ie)aftr erasing first one using backspace},only 2 digits are allowed aftr it.
plzzzz help me solve this.
 Preethi. 
-
Feb 16th, 2007, 11:19 AM
#2
Re: Again with Decimal...........
Forgot who made a numerical textbox control... try doing a search for it.
-
Feb 16th, 2007, 11:21 AM
#3
Re: Again with Decimal...........
Try something like
VB Code:
If KeyAscii = 46 Then
If InStr(1, Text1.Text, ".") = 0 Then 'there is no decimal point
'do whatever you need to do if there is no decimal point
Else
'there is a decimal point, so whatever is appopriate here
End If
End If
-
Feb 16th, 2007, 11:26 AM
#4
Re: Again with Decimal...........
this does it.. just whipped it up.. im sure it could be better
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 And KeyAscii <> 46 Then
KeyAscii = 0
Exit Sub
End If
If InStr(Text1, ".") And KeyAscii = 46 Then
KeyAscii = 0
Exit Sub
End If
If InStr(Text1, ".") And KeyAscii <> 8 Then
If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
KeyAscii = 0
Exit Sub
End If
End If
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 16th, 2007, 12:08 PM
#5
Re: [RESOLVED] Again with Decimal...........
Hi Static , it is a beautiful code but I found a bug.
When you run this code do the following
Enter 123456 in the textbox and then using the mouse place the cursor between 3 and 4.
Now if you insert a decimal, it will let you do it so the idea of having two decimal places fails...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 16th, 2007, 01:27 PM
#6
Re: [RESOLVED] Again with Decimal...........
ok.. its not perfect i know.. i did it quickly.
here.. happy now?
VB Code:
Private Function Nums(KA As Integer) As Integer
Nums = KA
If Not IsNumeric(Chr(KA)) And KA <> 8 And KA <> 46 Then
Nums = 0
Exit Function
End If
If InStr(Text1, ".") And KA = 46 Then
Nums = 0
Exit Function
End If
If InStr(Text1, ".") And KA <> 8 Then
If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
Nums = 0
Exit Function
End If
ElseIf InStr(Text1, ".") = 0 And KA = 46 Then
If Text1.SelStart < Len(Text1) - 2 Then
Nums = 0
End If
End If
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = Nums(KeyAscii)
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 16th, 2007, 01:34 PM
#7
Re: [RESOLVED] Again with Decimal...........
ok.. its not perfect i know.. i did it quickly.
here.. happy now?
Awesome is the word
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
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
|