Jump to content
  • Advertisement
Sign in to follow this  
metal_kid43

VB 2005 Xpress KeyPress Help

This topic is 4233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im creating a Windows-like calculator application and I can't quite figure out how to put the number into a text box. For example, if the user enters 5 on the keyboard, I want 5 to be placed in the box. I know it has something to do with the keypress event, but I think my usage of it is a bit off. . . Thanks! P.S. The code in question is at the bottom...
Public Class frmCalculator
    Dim decNum1 As Decimal
    Dim decNum2 As Decimal
    Dim bytSignState As Byte
    Dim bytNumState As Byte
    Dim decResult As Decimal

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtBox.Text = ""
        decNum1 = 0
        decNum2 = 0
        decResult = 0
        bytNumState = 0
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 0
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 0
            txtBox.Text = CStr(decNum2)
        End If
        
    End Sub

    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 1
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 1
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 2
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 2
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 3
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 3
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 4
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 4
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 5
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 5
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 6
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 6
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 7
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 7
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 8
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 8
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        If bytNumState = 0 Then
            decNum1 = txtBox.Text & 9
            txtBox.Text = CStr(decNum1)
            'bytNumState += 1
        Else
            decNum2 = txtBox.Text & 9
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
        decNum1 = CDec(txtBox.Text)
        txtBox.Text = ""
        bytSignState = 1
        bytNumState += 1
    End Sub

    Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
        decNum1 = CDec(txtBox.Text)
        txtBox.Text = ""
        bytSignState = 2
        bytNumState += 1
    End Sub

    Private Sub btnMult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMult.Click
        decNum1 = CDec(txtBox.Text)
        txtBox.Text = ""
        bytSignState = 3
        bytNumState += 1
    End Sub

    Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiv.Click
        decNum1 = CDec(txtBox.Text)
        txtBox.Text = ""
        bytSignState = 4
        bytNumState += 1
    End Sub

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        decNum2 = CDec(txtBox.Text)
        txtBox.Text = ""
        If bytSignState = 1 Then
            decResult = decNum1 + decNum2
        ElseIf bytSignState = 2 Then
            decResult = decNum1 - decNum2
        ElseIf bytSignState = 3 Then
            decResult = decNum1 * decNum2
        ElseIf bytSignState = 4 Then
            decResult = decNum1 / decNum2
        End If
        txtBox.Text = CStr(decResult)
    End Sub

    Private Sub btnPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPoint.Click
        If CStr(txtBox.Text).IndexOf(".") = -1 Then
            txtBox.Text = txtBox.Text & "."
        End If
    End Sub

    Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
        If bytNumState = 0 Then
            decNum1 = 0 - decNum1
            txtBox.Text = CStr(decNum1)
        Else
            decNum2 = 0 - decNum2
            txtBox.Text = CStr(decNum2)
        End If
    End Sub

    Private Sub frmCalculator_KeyPress(ByVal KeyAscii As Integer)
        txtBox.Text = CStr(KeyAscii)
    End Sub
End Class    

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!