Results 1 to 7 of 7

Thread: [RESOLVED] Again with Decimal...........

  1. #1

    Thread Starter
    Lively Member preethi_rjs's Avatar
    Join Date
    Dec 2006
    Posts
    80

    Resolved [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:
    1. Option Explicit
    2. Dim k, s, c
    3.  
    4. Private Sub Text1_Change()
    5. If Len(Text1.Text) = s Then
    6. Command1.SetFocus
    7. End If
    8. End Sub
    9.  
    10. Private Sub Text1_KeyPress(KeyAscii As Integer)
    11. k = Chr(KeyAscii) Like "[0-9]"
    12.  
    13. If KeyAscii = 46 And c >= 1 Then
    14. KeyAscii = 0
    15. End If
    16.  
    17. If KeyAscii = 46 Then
    18. s = Len(Text1.Text) + 3
    19. c = c + 1
    20. End If
    21. 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.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Again with Decimal...........

    Forgot who made a numerical textbox control... try doing a search for it.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Again with Decimal...........

    Try something like
    VB Code:
    1. If KeyAscii = 46 Then
    2.    If InStr(1, Text1.Text, ".") = 0 Then 'there is no decimal point
    3.       'do whatever you need to do if there is no decimal point  
    4.    Else
    5.       'there is a decimal point, so whatever is appopriate here
    6.    End If
    7. End If

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Again with Decimal...........

    this does it.. just whipped it up.. im sure it could be better

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 And KeyAscii <> 46 Then
    3.         KeyAscii = 0
    4.         Exit Sub
    5.     End If
    6.     If InStr(Text1, ".") And KeyAscii = 46 Then
    7.         KeyAscii = 0
    8.         Exit Sub
    9.     End If
    10.     If InStr(Text1, ".") And KeyAscii <> 8 Then
    11.         If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
    12.             KeyAscii = 0
    13.             Exit Sub
    14.         End If
    15.     End If
    16. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: [RESOLVED] Again with Decimal...........

    ok.. its not perfect i know.. i did it quickly.
    here.. happy now?

    VB Code:
    1. Private Function Nums(KA As Integer) As Integer
    2.     Nums = KA
    3.     If Not IsNumeric(Chr(KA)) And KA <> 8 And KA <> 46 Then
    4.         Nums = 0
    5.         Exit Function
    6.     End If
    7.     If InStr(Text1, ".") And KA = 46 Then
    8.         Nums = 0
    9.         Exit Function
    10.     End If
    11.     If InStr(Text1, ".") And KA <> 8 Then
    12.         If Len(Mid(Text1, InStr(Text1, ".") + 1, 2)) = 2 Then
    13.             Nums = 0
    14.             Exit Function
    15.         End If
    16.     ElseIf InStr(Text1, ".") = 0 And KA = 46 Then
    17.         If Text1.SelStart < Len(Text1) - 2 Then
    18.             Nums = 0
    19.         End If
    20.     End If
    21.    
    22. End Function
    23.  
    24. Private Sub Text1_KeyPress(KeyAscii As Integer)
    25.     KeyAscii = Nums(KeyAscii)
    26. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
  •  



Click Here to Expand Forum to Full Width