I am currently using Visual Basic 2010 and am making a basic calculator. It should recognize keystrokes from the numpad and treat them like the buttons on my calculator used for numbers and operations. However, I cannot get it to work.
For example, I'd like my program to display the number 4 in the command line when numpad 4 is pressed and am trying to do it like this:
Private Sub BeforeLoad(sender As System.Object, e As System.EventArgs) Handles MyBase.Load, Me.KeyDown
Me.KeyPreview = True
If Keys.NumPad4 = True Then
If (Input AndAlso txtDisplay.Text <> "0") Or Point Then
txtDisplay.Text += btn4.Text
Else
txtDisplay.Text = btn4.Text
Input = True
End If
End If
End Sub
However, when I start my program and try to click numpad4, nothing shows up. Any help into this matter would be greatly appreciated. [Solved]
My other problem is that the enter button returns a value of '1' on the numpad instead of calculating the answer that I'd like it to.
Case Keys.Enter
EnterPress(btnEqual)
Private Sub EnterPress(btn As Button)
'# Enter Button
If txtDisplay.Text.Length <> 0 Then
CalculateTotals()
Operation = String.Empty
Point = False
End If
End Sub
My code in its entirety can be found here: http://pastebin.com/dsMCNy3i
Here is the code to my equals button:
Private Sub Master(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
If ModOp = True Then
Num2 = Val(txtDisplay.Text)
txtDisplay.Text = Num1 Mod Num2
ElseIf txtDisplay.Text.Length <> 0 Then
CalculateTotals()
Operation = String.Empty
Point = False
End If
End Sub
It appears I've found two solutions to this.
1) Put a '&' symbol before the number in the button's text in the properties window.
2) This code worked for me:
Private Sub Keystrokes(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad1
btn1.PerformClick()
Case Keys.NumPad2
btn2.PerformClick()
End Sub