3 Replies - 18349 Views - Last Post: 09 November 2007 - 04:14 PM Rate Topic: -----

#1 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 324
  • Joined: 08-November 07

Binding key press on keypad to button

Posted 08 November 2007 - 08:02 AM

Alrighty, so here is the code I'm working with. It works perfectly fine if I click the buttons on the form, but what I need it to do is pick up the keypresses from the keypad like 0-9, ., and all the numeric operators. Main reason I'm wrote this is that on my workstation, I don't have a calculator and don't have access to the SP2 cds to access i386 for the files to import calc and paint and a few others. I've already written the paint program now I just need this small snippet to be able to use the keypad to enter numbers. If it's not possible that's cool, just let me know. I've only been coding in VB.Net for about a month now.

Private Accum As String
	Private NumQueue As New Queue
	Private OpQueue As New Queue

	Private Sub op_Click(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles opAdd.Click, opDivide.Click, _
	 opMultiply.Click, opSubtract.Click
		OpQueue.Enqueue(sender.Tag)
		NumQueue.Enqueue(Accum)
		Accum = ""
	End Sub

	Private Sub opEquals_Click(ByVal sender As Object, _
	 ByVal e As System.EventArgs) Handles opEquals.Click
		Dim CurrentOp As String
		Dim Num1, Num2 As Double
		NumQueue.Enqueue(Accum)
		Try
			Num1 = CDbl(NumQueue.Dequeue)
			Num2 = CDbl(NumQueue.Dequeue)
			Do
				CurrentOp = OpQueue.Dequeue
				Select Case CurrentOp
					Case "Divide"
						If Num2 <> 0 Then
							Num1 /= CDbl(Num2)
						Else
							txtCalc.Text = "Divide By Zero"
							Exit Sub
						End If
					Case "Multiply"
						Num1 *= CDbl(Num2)
					Case "Add"
						Num1 += CDbl(Num2)
					Case "Subtract"
						Num1 -= CDbl(Num2)
				End Select
				If NumQueue.Count = 0 Then Exit Do
				Num2 = NumQueue.Dequeue
			Loop
			txtCalc.Text = CStr(Num1)
			Accum = ""
		Catch
			OpQueue.Clear()
			NumQueue.Clear()
			Accum = ""
			txtCalc.Text = "0"
			Exit Sub
		End Try
	End Sub

	Private Sub btnClick(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, btnDec.Click
		If InStr(Accum, ".") <> 0 Then
			Exit Sub
		End If
		If Len(Accum) = 0 Then
			' First keypress
			Accum = sender.Tag
		Else
			' Second or later keypress
			Accum &= sender.Tag
		End If
		txtCalc.Text = Accum
	End Sub

	Private Sub txtClearAll_Click(ByVal sender As Object, _
	ByVal e As System.EventArgs) Handles txtClearAll.Click
		txtCalc.Text = "0"
		Accum = ""
		If sender.Tag = "Clear" Then
			OpQueue.Clear()
			NumQueue.Clear()
		End If
	End Sub


edit: bah I just found the calculator tutorial in the VB.Net tutorials section

This post has been edited by nofear217: 08 November 2007 - 10:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Binding key press on keypad to button

#2 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 324
  • Joined: 08-November 07

Re: Binding key press on keypad to button

Posted 08 November 2007 - 10:52 AM

Well, I did learn some things from the calc tutorial but still waiting for the follow up to show how to bind the keypresses of numberpad in, but will keep looking through the other tutorials.
Was This Post Helpful? 0
  • +
  • -

#3 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 324
  • Joined: 08-November 07

Re: Binding key press on keypad to button

Posted 09 November 2007 - 08:22 AM

Any idea PsychoCoder? I saw it was your tutorial but couldn't find the follow up that showed how to do what I'm asking about.
Was This Post Helpful? 0
  • +
  • -

#4 Kaysakado  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-November 07

Re: Binding key press on keypad to button

Posted 09 November 2007 - 04:14 PM

Well, this doesn't actually make it press the key on key press, but it should work(This'll make it run the code when you press "Q", if you couldn't tell):
Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Test()
	End Sub

	Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
		If e.KeyChar = Chr(Keys.Q) Then
			Test()
		End If
	End Sub
	Private Function Test()
		MsgBox("Test")
	End Function
End Class

This post has been edited by Kaysakado: 09 November 2007 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1