Results 1 to 18 of 18

Thread: Numeric Textbox with a Fixed Decimal Place – Like a Calculator

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Numeric Textbox with a Fixed Decimal Place – Like a Calculator

    This is code I wrote for a TextBox that only accepts numeric entries. Numbers are entered from right to left with a fixed two place decimal. It’s like a calculator where input follows the format: .01, .12, 1.23, 12.34, etc. The decimal is automatically entered and can be replaced with a “:” for Time TextBoxes. All numbers are entered and removed from the right side. Numbers are removed with the Delete Key or the Backspace. This code uses the KeyDown Event, so copying and pasting into the TextBox is disabled.

    Code:
    Public Class Form1
        '
        ' Modified 1/6/10 to include support for numberpad
        Dim d As Integer ' index of decimal or colon
        Dim theText As String ' String that calculates the new texbox value
        Dim theNum As String ' The Number that was entered - Either from the keyboard or the numberpad
        Dim numSep As String = "." ' Selected seperator - Can be . or : 
        '
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Set total length and right alignment of TextBox and Disable Copy/Paste
            TextBox1.TextAlign = HorizontalAlignment.Right
            TextBox1.MaxLength = 5
            TextBox1.ShortcutsEnabled = False ' Disable Right-Click Context Menu - No Pasting Allowed - Ctrl-V disabled because V is invalid
        End Sub
    
    
        Private Sub TextBox1_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
            ' Textbox will accept numbers only in the format 20.45 or 20:45 - Seperator is automaically entered
            ' Set MaxLength Property to control length
            ' All numbers are entered from right to left
            ' Use Back Space or Del Key to remove numbers starting from right side
            ' Right now only supports 2 decimal places
            ' Paste is disabled - pasting text into textbox would crash app
            '
            theText = String.Empty
            '
            If (((e.KeyData >= 48 And e.KeyData <= 57) Or (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9)) And Len(TextBox1.Text) < TextBox1.MaxLength) Or e.KeyData = Keys.Back Or e.KeyData = Keys.Delete Then
                '
                If e.KeyData <> Keys.Back And e.KeyData <> Keys.Delete Then
                    '
                    If e.KeyData >= 48 And e.KeyData <= 57 Then
                        ' Keyboard Number Entry
                        theNum = Chr(e.KeyData)
                    Else
                        ' Keyboard NumberPad Entry
                        theNum = Microsoft.VisualBasic.Right(e.KeyData.ToString, 1)
                    End If
                    '
                    TextBox1.Text = TextBox1.Text & theNum
                    '
                    If Len(TextBox1.Text) >= 2 Then
                        If Len(TextBox1.Text) = 2 Then ' Apply the seperator
                            TextBox1.Text = numSep & TextBox1.Text
                        Else
                            ' Append new number to right and shift everything to the left 
                            d = TextBox1.Text.IndexOf(numSep)
                            theText = Microsoft.VisualBasic.Left(TextBox1.Text, d) & TextBox1.Text.Substring(d + 1, 1) & numSep & TextBox1.Text.Substring(d + 2, 1) & theNum
                            '
                            If Microsoft.VisualBasic.Left(theText, 1) = "0" Then
                                ' Remove leading '0' if going from .01 to .12 - don't want 0.12
                                theText = theText.Remove(0, 1)
                            End If
                            '
                            TextBox1.Text = theText
                            '
                        End If
                    End If
                    '
                Else
                    ' Remove far right number and shift everything to the right
                    If Len(TextBox1.Text) > 3 Then
                        d = TextBox1.Text.IndexOf(numSep)
                        theText = Microsoft.VisualBasic.Left(TextBox1.Text, d - 1) & numSep & TextBox1.Text.Substring(d - 1, 1) & TextBox1.Text.Substring(d + 1, 1)
                    Else
                        If Len(TextBox1.Text) = 3 Then
                            theText = TextBox1.Text.Substring(1, 1)
                        Else
                            theText = String.Empty
                        End If
                    End If
                    '
                    TextBox1.Text = theText
                    '
                End If
                    ' 
            End If
            '
            ' Always suppress the key pressed, if key entered was valid - this event handles it - otherwise suppress it
            e.SuppressKeyPress = True
            ''
        End Sub
    
        ' When leaving the textbox, if the length is 1 pad with a 0 or clear it if the value is 0 or 00
        Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
            '
            If Len(TextBox1.Text) = 1 Then
                If TextBox1.Text <> "0" Then
                    TextBox1.Text = numSep & "0" & TextBox1.Text
                Else
                    TextBox1.Text = String.Empty
                End If
            End If
            '
            If Len(TextBox1.Text) = 3 And TextBox1.Text = numSep & "00" Then
                TextBox1.Text = String.Empty
            End If
    
        End Sub
    
    End Class
    Last edited by dkahn; Jan 8th, 2010 at 09:12 PM.

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