use the numberpad as input?

junkie_ball

Member
Joined
Mar 25, 2010
Messages
19
Programming Experience
1-3
Hi All,

I have created a simple calculator in VB.net 2010 and whilst i have basic functions and calculations working correctly i cannot figure out how to use the numberpad as a means of input rather than by clicking on the buttons i have placed on the form for each digit.

Is there a simple way of asigning a key on the keyboard to activate a click event of a button on my form?
 
Easiest way, use the Form's KeyUp event, check e.KeyCode against the Keys enum for the one's you want to catch, then simply call your input sub (the sub that handles all of the buttons on the form) passing it the appropriate number.
 
Is there a simple way of asigning a key on the keyboard to activate a click event of a button on my form?
Actually, there is, just set text of button to &1 for the 1 number etc.
This goes under the name of access keys o mnemonics.
 
Calculator

Hi,

Here's a calculator I wrote once.
You can use the mouse and/or keys.

CU, Freddy Mellaerts
VB.NET:
Option Explicit On

Imports System.Math
Imports System.Windows.Forms
Imports Microsoft.VisualBasic

Public Class Form1
    Dim Action As String = String.Empty
    Dim HasResult As Boolean = False

    Dim Mem As Double = 0
    Dim Value1 As Double = 0
    Dim Value2 As Double = 0

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Action = "+"
        Value1 = CDbl(txtNumbers.Text)
        txtNumbers.Text = "0"

    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

        Action = "/"
        Value1 = CDbl(txtNumbers.Text)
        txtNumbers.Text = "0"

    End Sub

    Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

        Value2 = CDbl(txtNumbers.Text)

        Select Case Action
            Case "+"
                txtNumbers.Text = Value1 + Value2
            Case "-"
                txtNumbers.Text = Value1 - Value2
            Case "/"
                txtNumbers.Text = Value1 / Value2
            Case "*"
                txtNumbers.Text = Value1 * Value2
        End Select

        HasResult = True

    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

        Action = "*"
        Value1 = CDbl(txtNumbers.Text)
        txtNumbers.Text = "0"

    End Sub

    Private Sub btnOneDivideBy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOneDivideBy.Click

        txtNumbers.Text = CStr(1 / (Val(txtNumbers.Text)))

        HasResult = True

    End Sub

    Private Sub btnPercentage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercentage.Click

        txtNumbers.Text = Value1 * Val(txtNumbers.Text) / 100

    End Sub

    Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click

        txtNumbers.Text = CDbl(0 - Val(txtNumbers.Text))

    End Sub

    Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click

        txtNumbers.Text = CStr(Sqrt(Val(txtNumbers.Text)))

        HasResult = True

    End Sub

    Private Sub btnSubstract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubstract.Click

        Action = "-"
        Value1 = CDbl(txtNumbers.Text)
        txtNumbers.Text = "0"

    End Sub

    Private Sub ButtonC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click

        txtNumbers.Text = "0"
        Value1 = 0
        Value2 = 0
        Action = ""

    End Sub

    Private Sub Digit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDecimal.Click

        If HasResult Then btnC.PerformClick() : HasResult = False

        If txtNumbers.Text = "0" Then
            txtNumbers.Text = sender.text
        Else
            txtNumbers.Text &= sender.text
        End If

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode

            Case Keys.Escape : ButtonC_Click(btnC, e)
            Case Keys.Back : If txtNumbers.Text.Length > 0 Then txtNumbers.Text = txtNumbers.Text.Remove(txtNumbers.Text.Length - 1, 1) : Value1 = CDbl(txtNumbers.Text)
            Case Keys.Enter : btnEqual.PerformClick()

            Case Keys.Add : Action = "+" : Value1 = CDbl(txtNumbers.Text) : txtNumbers.Text = "0"
            Case Keys.Subtract : Action = "-" : Value1 = CDbl(txtNumbers.Text) : txtNumbers.Text = "0"
            Case Keys.Divide : Action = "/" : Value1 = CDbl(txtNumbers.Text) : txtNumbers.Text = "0"
            Case Keys.Multiply : Action = "*" : Value1 = CDbl(txtNumbers.Text) : txtNumbers.Text = "0"

            Case Keys.Decimal : Digit(btnDecimal, e)
            Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)
            Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
            Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
            Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
            Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
            Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
            Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
            Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
            Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
            Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)

        End Select

    End Sub

End Class
 
Hi,

Here's a calculator I wrote once.
You can use the mouse and/or keys.

CU, Freddy Mellaerts

Thanks for the sample code whilst my calculator worked pretty much the same as your code yours was so much neater than mine. I have now had time to update and streamline my code. I am still having a problem with the keyboard entry. I've used the forms keydown event and coded my buttons with the appropiate keys allow when running the program the keyboard inputs do not work. Do i need to call the keyboard entry somewhere.

I appreciate your time for a newbie and self learner i have posted my revised code below.

VB.NET:
Option Explicit On

Imports System.Math
Imports System.Windows.Forms
Imports Microsoft.VisualBasic


Public Class frmCalculator

    Dim HasResult As Boolean = False

    Dim FirstNumber As Single
    Dim SecondNumber As Single
    Dim Arthemetic As String

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "+"

    End Sub

    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "-"

    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "X"

    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "/"

    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

        SecondNumber = Val(lblResult.Text)

        Select Case Arthemetic

            Case "+"
                lblResult.Text = FirstNumber + SecondNumber
            Case "-"
                lblResult.Text = FirstNumber - SecondNumber
            Case "X"
                lblResult.Text = FirstNumber * SecondNumber
            Case "/"
                lblResult.Text = FirstNumber / SecondNumber
                If SecondNumber = 0 Then
                    lblResult.Text = "Cannot Divide By Zero"
                    Exit Sub
                End If

        End Select

        HasResult = True


    End Sub

    Private Sub btnDecimalPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimalPlace.Click

        If lblResult.Text.IndexOf(".") >= 0 Then
            lblResult.Text = lblResult.Text
        Else
            lblResult.Text = lblResult.Text & "."
        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lblResult.Text = "0"
        FirstNumber = 0
        SecondNumber = 0
        Arthemetic = ""

    End Sub

    Private Sub btnClearError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearError.Click

        lblResult.Text = "0"

        If Arthemetic > "" Then
            SecondNumber = 0
        End If

    End Sub

    Private Sub Digit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click

        If HasResult Then btnClear.PerformClick() : HasResult = False

        If lblResult.Text = "0" Then
            lblResult.Text = sender.text
        Else
            lblResult.Text &= sender.text
        End If

    End Sub


    Private Sub frmCalculator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode

            Case Keys.Escape : btnClear_Click(btnClear, e)
            Case Keys.Back : If lblResult.Text.Length > 0 Then lblResult.Text = lblResult.Text.Remove(lblResult.Text.Length - 1, 1) : FirstNumber = CSng(lblResult.Text)
            Case Keys.Enter : btnEquals.PerformClick()

            Case Keys.Add : Arthemetic = "+" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
            Case Keys.Subtract : Arthemetic = "-" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
            Case Keys.Divide : Arthemetic = "/" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
            Case Keys.Multiply : Arthemetic = "*" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"


            Case Keys.Decimal : btnDecimalPlace.PerformClick()
            Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
            Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
            Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
            Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
            Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
            Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
            Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
            Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
            Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)
            Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)

        End Select

    End Sub
End Class
 
Hi,

Don't worry. I'm happy I could help you.
If you have more questions, just ask them.
I'm a professional programmer, but I like to help people if I have the time.
I am a Belgian man, living in Mexico.

CU, Freddy Mellaerts
 
Hi again,

Ok i have the calculator working pretty much the way i want now and looking at adding more complex functions in an advanced view of the calculator. Prior to doing this i have one bug i have poored over for hours. When i try to use the enter key to perform the button equals click it performs the action of whichever button has the focus. This seems to be the same with the space bar. Are these special reserved keys? I've attached my code below. I cannot figure this out for the life of me i'm sure its a simple bug. Has this happened to anyone?

VB.NET:
Option Explicit On

Imports System.Math
Imports System.Windows.Forms
Imports Microsoft.VisualBasic


Public Class frmCalculator

    Dim HasResult As Boolean = False

    Dim FirstNumber As Single
    Dim SecondNumber As Single
    Dim Arthemetic As String

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        'Sets the values to perform a addition equation & calls equation function

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "+"
        Call EquationText()

    End Sub

    Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click

        'Sets the values to perform a subtraction equation & calls equation function

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "-"
        Call EquationText()

    End Sub

    Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

        'Sets the values to perform a multiply equation & calls equation function

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "X"
        Call EquationText()

    End Sub

    Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

        'Sets the values to perform a division equation & calls equation function

        FirstNumber = Val(lblResult.Text)
        lblResult.Text = "0"
        Arthemetic = "/"
        Call EquationText()

    End Sub

    Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

        'Sets the second number and performs the four standard equations addition, subtraction, multiplication and division

        SecondNumber = Val(lblResult.Text)

        Select Case Arthemetic

            Case "+"
                lblResult.Text = FirstNumber + SecondNumber
            Case "-"
                lblResult.Text = FirstNumber - SecondNumber
            Case "X"
                lblResult.Text = FirstNumber * SecondNumber
            Case "/"
                lblResult.Text = FirstNumber / SecondNumber
                If SecondNumber = 0 Then
                    lblResult.Text = "Cannot Divide By Zero"
                    Exit Sub
                End If

        End Select

        lblEquation.Text = "0"

        HasResult = True


    End Sub

    Private Sub btnDecimalPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimalPlace.Click

        'Inserts a decimal place and checks to make sure only one decimal place can be placed in each number

        If lblResult.Text.IndexOf(".") >= 0 Then
            lblResult.Text = lblResult.Text
        Else
            lblResult.Text = lblResult.Text & "."
        End If

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        'Clears all current values in various functions

        lblResult.Text = "0"
        lblEquation.Text = "0"
        FirstNumber = 0
        SecondNumber = 0
        Arthemetic = ""
        lblEquation.Text = "0"

    End Sub

    Private Sub btnClearError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearError.Click

        'Clears only the current number in the equation so number can be updated without clearing the whole equation

        lblResult.Text = "0"

        If Arthemetic > "" Then
            SecondNumber = 0
        Else
            FirstNumber = 0
        End If

    End Sub

    Private Sub Digit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click

        'Controls the click event for keyboard input

        If HasResult Then btnClear.PerformClick() : HasResult = False

        If lblResult.Text = "0" Then
            lblResult.Text = sender.text
        Else
            lblResult.Text &= sender.text
        End If



    End Sub


    Private Sub frmCalculator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        'Sets the keyboard values to allow the user to enter data direct from the keyboard

        Select Case e.KeyCode

            Case Keys.Escape : btnClear_Click(btnClear, e)
            Case Keys.Back : If lblResult.Text.Length > 0 Then lblResult.Text = lblResult.Text.Remove(lblResult.Text.Length - 1, 1) Else lblResult.Text = 0 : FirstNumber = CSng(lblResult.Text)


                'Sets the values to perform a equation & calls equation function

            Case Keys.Add : Arthemetic = "+" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
            Case Keys.Subtract : Arthemetic = "-" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
            Case Keys.Divide : Arthemetic = "/" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()
            Case Keys.Multiply : Arthemetic = "X" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0" : Call EquationText()

            Case Keys.Enter : btnEquals.PerformClick() 'btnEquals_Click(btnEquals, e)

                'Captures keyboard input for each number

            Case Keys.Decimal : btnDecimalPlace.PerformClick()
            Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
            Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
            Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
            Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
            Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
            Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
            Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
            Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
            Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)
            Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)



        End Select

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        'Exit the program

        Me.Close()

    End Sub

    Private Sub BasicToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BasicToolStripMenuItem.Click

        'Resizes the form to display the basic calculator

        Me.Size = New System.Drawing.Size(245, 366)

    End Sub

    Private Sub frmCalculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Resizes the form to display the basic calculator as default and display the square root icon on the button

        Me.Size = New System.Drawing.Size(254, 366)
        btnSquareRoot.Text = Char.ConvertFromUtf32(Integer.Parse("221A", Globalization.NumberStyles.HexNumber))

    End Sub

    Private Sub AdvancedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdvancedToolStripMenuItem.Click

        'Resizes the form to display the Advanced calculator

        Me.Size = New System.Drawing.Size(378, 366)

    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

        'Displays the About screen

        SplashScreen1.Show()

    End Sub


    Private Sub btnSquareRoot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSquareRoot.Click

        'Performs the square root calculation

        If lblResult.Text <= 0 Then
            MsgBox("Cannot Find Square Root Of Negative Number", MsgBoxStyle.Information)
            lblResult.Text = 0
        Else
            lblResult.Text = CStr(Sqrt(Val(lblResult.Text)))
        End If

        HasResult = True

    End Sub

    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPercent.Click

        'Performs the Percentage Calculation

        lblResult.Text = FirstNumber * Val(lblResult.Text) / 100

    End Sub

    Private Sub EquationText()

        ' Displays the current equation in the equation label

        lblEquation.Text = Str(FirstNumber) + " " + Arthemetic

    End Sub
End Class
 
Are these special reserved keys?
Yes, they are special dialog/navigation keys.
VB.NET:
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
    Select Case keyData
        Case Keys.Enter, Keys.Space
            Me.AcceptButton.PerformClick()
            Return True
    End Select
    Return MyBase.ProcessDialogKey(keyData)
End Function
Control.ProcessDialogKey Method (System.Windows.Forms)
 
Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)
Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)
You don't need these, see my reply in post 3.
And if you do want to write this code even still, use buttons PerformClick method rather than hacking the event handler.
 
JohnH Thanks for the info the protected overrides worked a treat for using 'enter' as the equals on the keyboard. I can also see the logic of using the & infront of the numbers in text properties. This is why i became a member here to try and learn a more effiecient way of coding. I realise there are a number of ways to achieve the same results in VB wether through code of form design. I usually go the long way about it at the moment as a complete novice.

I must say the less code to accomplish a task the better means less debugging so thanks for the info and help on this again.
 
You don't need these, see my reply in post 3.
And if you do want to write this code even still, use buttons PerformClick method rather than hacking the event handler.

I have tried setting the buttons text for example '&1' and '&2' whilst this allows me to enter the numbers using the keyboard directly without code the keyboard it seems to actually creates the string including the & symbols. For example if i type 123 on the keyboard i would get '&1&2&3' as a string in my code (allow the label control will only display 123) causing an error when i try to perform a calculation. Is there a simple way around this or am i better off still capturing the key stokes in code?
 
Could you set the numbers in the buttons Tag and use that?
 
Back
Top