How to create a backspace button in VB 2008

Posted on 2009-02-16
Last Modified: 2012-05-06

I have created a simple calculator for class with a few issues yet to be resolved.  I need to implement the following features:  +/- button, a decimal (only once), and a backspace.  I found this resource online that seems to address some of these issues i'm having but do not know how to implement it.  Can someone help me or give me clues as to how or where i should look to try to solve this?

Question by:khaji00
    1 Comment
    LVL 26

    Accepted Solution

    Try thi, from

    Private nonNumberEntered As Boolean = False

        ' Handle the KeyDown event to determine the type of character entered into the control.
        Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
             Handles TextBox1.KeyDown
            ' Initialize the flag to false.
            nonNumberEntered = False

            ' Determine whether the keystroke is a number from the top of the keyboard.
            If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
                ' Determine whether the keystroke is a number from the keypad.
                If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
                    ' Determine whether the keystroke is a backspace.
                    If e.KeyCode <> Keys.Back Then
                        If (e.KeyCode <> Keys.OemPeriod) Then
                            ' A non-numerical keystroke was pressed.
                            ' Set the flag to true and evaluate in KeyPress event.
                            nonNumberEntered = True
                            If (InStr(TextBox1.Text, ".") > 0) Then
                                nonNumberEntered = True
                                nonNumberEntered = False
                            End If
                        End If
                    End If
                End If
            End If
        End Sub 'textBox1_KeyDown

        ' This event occurs after the KeyDown event and can be used
        ' to prevent characters from entering the control.
        Private Sub textBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
            Handles TextBox1.KeyPress
            ' Check for the flag being set in the KeyDown event.
            If nonNumberEntered = True Then
                ' Stop the character from being entered into the control since it is non-numerical.
                e.Handled = True
            End If
        End Sub 'textBox1_KeyPress

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now