Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to create a backspace button in VB 2008

Posted on 2009-02-16
Medium Priority
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

EDDYKT earned 2000 total points
ID: 23658563
Try thi, from http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

581 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