One decimal point, and 2 numbers :)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iDaz
    New Member
    • Feb 2007
    • 5

    One decimal point, and 2 numbers :)

    hi everyone!
    i've searched many sites including this one for the answer to me question, but i'm still in a bit of trouble. this is my current code...
    Code:
     Private Sub unitPriceKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles unitPriceTextBox.KeyPress
            If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or e.KeyChar = ".") Then
                e.Handled = True
                MessageBox.Show("A price can only include numeric digits.", "Numeric Characters Only", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End Sub
    everything works fine! BUT, i only want my unitPriceTextBo x to accept ONE decimal point, and only allow TWO numbers maximum after the decimal point, eg. 1234.56 or 45.9 or 45.90. at the moment it obviously accepts unlimited decimal points and allow unlimited numbers after each decimal point.

    if anyone could add to my code, or give me new code to help me out, i would greatly appreciate it!

    thank you in advance.
    :)
    Last edited by willakawill; Feb 24 '07, 07:00 AM. Reason: please use code tags when posting code
  • hariharanmca
    Top Contributor
    • Dec 2006
    • 1977

    #2
    Originally posted by iDaz
    hi everyone!
    i've searched many sites including this one for the answer to me question, but i'm still in a bit of trouble. this is my current code...
    Private Sub unitPriceKeyPre ss(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles unitPriceTextBo x.KeyPress
    If Not (Char.IsDigit(e .KeyChar) Or Char.IsControl( e.KeyChar) Or e.KeyChar = ".") Then
    e.Handled = True
    MessageBox.Show ("A price can only include numeric digits.", "Numeric Characters Only", MessageBoxButto ns.OK, MessageBoxIcon. Exclamation)
    End If
    End Sub

    everything works fine! BUT, i only want my unitPriceTextBo x to accept ONE decimal point, and only allow TWO numbers maximum after the decimal point, eg. 1234.56 or 45.9 or 45.90. at the moment it obviously accepts unlimited decimal points and allow unlimited numbers after each decimal point.

    if anyone could add to my code, or give me new code to help me out, i would greatly appreciate it!

    thank you in advance.
    :)

    Code:
    Private Sub txtValue_KeyPress(KeyAscii As Integer)
        KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
    End Sub
    
    Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
        If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn Then Check_Decimal = pKeyascii: Exit Function
        If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0: Exit Function
        If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0: Exit Function
        If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
            If pOptNoDecimal > 0 Then
                Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
            Else
                Check_Decimal = pKeyascii
            End If
        Else
            If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
            Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
        End If
    End Function

    Use this code in Key press Event

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Would you kindly post an explanation for this code snippet and some comments? Thanks

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by willakawill
        Would you kindly post an explanation for this code snippet and some comments? Thanks
        Code:
        Private Sub txtValue_KeyPress(KeyAscii As Integer)
            KeyAscii = Check_Decimal(KeyAscii, Trim(txtValue.Text), 4, 2)
        End Sub
        
        Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
            If pKeyascii = vbKeyBack Or pKeyascii = vbKeyReturn Then Check_Decimal = pKeyascii: Exit Function  ' Line 1
            'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
            If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0: Exit Function ' Line 2
            'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
            If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0: Exit Function ' Line 3
            'Line 3 - Check for the Key value is Numeric Only
            'Check for Decimal Acailable
            If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
            'True Part
                'Check for Decimal Number value
                If pOptNoDecimal > 0 Then
                    'True Part
                    'Get teh string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
                    Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
                Else
                    'False Part Return Key Value
                    Check_Decimal = pKeyascii
                End If
            Else
            'False Part
                'Check Number of Digit
                If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
                Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
            End If
        End Function
        Ok

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Excellent, thanks. It is the policy of TSDN not to post code without explanations.

          Comment

          • iDaz
            New Member
            • Feb 2007
            • 5

            #6
            THANK YOU VERY MUCH FOR YOUR HELP!!!!!

            i really do appreicate it

            Comment

            • iDaz
              New Member
              • Feb 2007
              • 5

              #7
              hariharanmca i have an error with your code

              Code:
              Public Function Check_Decimal(pKeyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
                      If pKeyascii = pKeyascii = Keys.Back Or pKeyascii = Keys.Enter Then Check_Decimal = pKeyascii : Exit Function ' Line 1
                      'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
                      If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0 : Exit Function ' Line 2
                      'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
                      If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0 : Exit Function ' Line 3
                      'Line 3 - Check for the Key value is Numeric Only
                      'Check for Decimal Acailable
                      If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
                          'True Part
                          'Check for Decimal Number value
                          If pOptNoDecimal > 0 Then
                              'True Part
                              'Get the string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
                              Check_Decimal = IIf(Len(Mid(Trim(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrValue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
                          Else
                              'False Part Return Key Value
                              Check_Decimal = pKeyascii
                          End If
                      Else
                          'False Part
                          'Check Number of Digit
                          If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
                          Check_Decimal = IIf(Len(pStrValue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
                      End If
                  End Function
              in the first line, the comma after Integer...

              As Integer, Optional pOptNoDecimal

              has a blue wiggled underline, and my error says "Optional parameters must specify a value"

              can you please help me out?

              thanks in advance!
              Last edited by willakawill; Feb 25 '07, 05:57 PM. Reason: please use code tags when posting code

              Comment

              • hariharanmca
                Top Contributor
                • Dec 2006
                • 1977

                #8
                Originally posted by iDaz
                hariharanmca i have an error with your code

                Public Function Check_Decimal(p Keyascii As Integer, pStrValue As String, Optional pOptNoDigit As Integer, Optional pOptNoDecimal As Integer) As Integer
                If pKeyascii = pKeyascii = Keys.Back Or pKeyascii = Keys.Enter Then Check_Decimal = pKeyascii : Exit Function ' Line 1
                'Line 1 - Check for the Key value is Enter or Backspace then Retutn KeyAscii Value
                If Chr(pKeyascii) = "." And InStr(1, pStrValue, ".") > 0 Then Check_Decimal = 0 : Exit Function ' Line 2
                'Line 2 - Check for the Key value is '. (Dot)' and already is there Dot then Retutn KeyAscii Value = 0
                If Not ((pKeyascii >= 48 And pKeyascii <= 57) Or pKeyascii = 46) Then Check_Decimal = 0 : Exit Function ' Line 3
                'Line 3 - Check for the Key value is Numeric Only
                'Check for Decimal Acailable
                If InStr(1, pStrValue & Chr(pKeyascii), ".") > 0 Then
                'True Part
                'Check for Decimal Number value
                If pOptNoDecimal > 0 Then
                'True Part
                'Get the string Length after Decimal If Greater than pOptNoDecimal Return Key value 0 else Return Return Key
                Check_Decimal = IIf(Len(Mid(Tri m(pStrValue), InStr(1, pStrValue & Chr(pKeyascii), "."), Len(Trim(pStrVa lue) - InStr(1, pStrValue & Chr(pKeyascii), ".")))) > pOptNoDecimal, 0, pKeyascii)
                Else
                'False Part Return Key Value
                Check_Decimal = pKeyascii
                End If
                Else
                'False Part
                'Check Number of Digit
                If pOptNoDigit <= 0 Then pOptNoDigit = Len(pStrValue & Chr(pKeyascii))
                Check_Decimal = IIf(Len(pStrVal ue & Chr(pKeyascii)) <= pOptNoDigit, pKeyascii, 0)
                End If
                End Function


                in the first line, the comma after Integer...

                As Integer, Optional pOptNoDecimal

                has a blue wiggled underline, and my error says "Optional parameters must specify a value"

                can you please help me out?

                thanks in advance!

                Of course, in .Net the Optional value need some default value at the time of declaration pOptNoDegit as integer = 0,pOptNoDecimal as Integer=0

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  if the return value of Check_Decimal>0 then keys.handle=fal se else true
                  is for .Net

                  Comment

                  Working...