0

基本上我已经找了好几个星期了,但我找不到一个使用小键盘作为计算器输入的好例子。

Private Sub Form1_KeyPress(ByVal KeyAscii As _MSForms.ReturnInteger)
    lblOutput.Text = lblOutput.Text & Chr(KeyAscii)
    If KeyAscii >= vbKeyNumpad0() And KeyAscii <= vbKeyNumpad9() Then
        lblOutput.Text = lblOutput.Text & Chr(KeyAscii)
    End If
End Sub
Option Explicit On

Public Class Form1
    Dim chrOperator As Char
    Dim varFirstVal As Integer

    Private Sub Form1_KeyPress(ByVal KeyAscii As _MSForms.ReturnInteger)
        lblOutput.Text = lblOutput.Text & Chr(KeyAscii)
        If KeyAscii >= vbKeyNumpad0() And KeyAscii <= vbKeyNumpad9() Then
            lblOutput.Text = lblOutput.Text & Chr(KeyAscii)
        End If
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        KeyPreview = True
    End Sub

    Private Sub ButtonAppend(number As Integer)
        If CInt(lblOutput.Text) = 0 Then
            lblOutput.Text = CType(number, String)
        Else
            lblOutput.Text = lblOutput.Text & number
        End If
    End Sub

    Private Sub operation(operand As String)
        chrOperator = CChar(operand)
        varFirstVal = CInt(lblOutput.Text)
        lblOutput.Text = CType(0, String)
    End Sub

    Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
        ButtonAppend(1)
    End Sub

    Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
        ButtonAppend(2)
    End Sub

    Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
        ButtonAppend(3)
    End Sub

    Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
        ButtonAppend(4)
    End Sub

    Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
        ButtonAppend(5)
    End Sub

    Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
        ButtonAppend(6)
    End Sub

    Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
        ButtonAppend(7)
    End Sub

    Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
        ButtonAppend(8)
    End Sub

    Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
        ButtonAppend(9)
    End Sub

    Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
        ButtonAppend(0)
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lblOutput.Text = "0"
        varFirstVal = 0
    End Sub

    Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
        Select Case chrOperator
            Case CChar("A")
                lblOutput.Text = CType(varFirstVal + CInt(lblOutput.Text), String)

            Case CChar("S")
                lblOutput.Text = CType(varFirstVal - CInt(lblOutput.Text), String)

            Case CChar("M")
                lblOutput.Text = CType(varFirstVal * CInt(lblOutput.Text), String)

            Case CChar("D")
                lblOutput.Text = CType(varFirstVal / CInt(lblOutput.Text), String)

        End Select
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        operation("A")
    End Sub

    Private Sub btnSub_Click(sender As Object, e As EventArgs) Handles btnSub.Click
        operation("S")
    End Sub

    Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
        operation("M")
    End Sub

    Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
        operation("S")
    End Sub

End Class

我想要摆脱它的是能够使用小键盘,而不必单击屏幕上的任何内容以使计算器工作。

4

0 回答 0