【问题标题】:giving calculator input from keyboard keys without pressing calculator buttons无需按计算器按钮即可从键盘按键输入计算器
【发布时间】:2014-05-18 11:06:54
【问题描述】:

我创建了一个 VB.net 计算器应用程序。它工作正常,但说我想添加 5+7。然后当我的应用程序工作时,我可以从键盘按 5,因为我将数字 5 的按钮文本设为 @987654321 @ 但是如果我从键盘上按 + 它就不起作用。我必须在计算器中按 + button。

我认为这是因为我的添加按钮被设计为将点击事件处理为btn_add_Click。有没有办法让这个应用程序工作,这样我就可以在不按计算器中的按钮的情况下按下键盘上的键并执行计算。这是我的代码:

Imports System.Math
Public Class Form1
    Private isFirstExist As Boolean
    Private inputOperator As String
    Private secondNum As Decimal
    Private firstNum As Decimal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCalc.TextChanged

End Sub

Private Sub btn_zero_Click(sender As System.Object, e As System.EventArgs) Handles btn_zero.Click
    removeFrontZero(0)

End Sub

Private Sub btn_one_Click(sender As System.Object, e As System.EventArgs) Handles btn_one.Click
    removeFrontZero(1)
End Sub

Private Sub btn_two_Click(sender As System.Object, e As System.EventArgs) Handles btn_two.Click
    removeFrontZero(2)
End Sub



Private Sub btn_clear_Click(sender As System.Object, e As System.EventArgs) Handles btn_clear.Click
    txtCalc.Clear()
    txtCalc.Text = "0"


End Sub


'Remove zero which is at the start
Public Sub removeFrontZero(ByVal digit As Integer)
    If txtCalc.Text = "0" Then
        txtCalc.Text = CStr(digit)
    Else
        txtCalc.Text &= digit
    End If
End Sub

Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
    inputOperator = "+"
    isFirst()
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    isFirstExist = False
End Sub

Private Sub btn_equal_Click(sender As System.Object, e As System.EventArgs) Handles btn_equal.Click
    If isFirstExist Then
        secondNum = CType(txtCalc.Text, Decimal)
    End If
    'Calculating the result
    Dim result As Decimal = calculate(firstNum, secondNum, inputOperator)
    txtCalc.Text = result.ToString()
    isFirstExist = False
End Sub

Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
    Dim output As Decimal
    firstNum = num1
    secondNum = num2
    Select Case inputOp
        Case "+"
            output = num1 + num2
        Case "-"
            output = num1 - num2

        Case "/"
            Dim value As Decimal
            Try
                isFirst()
                value = (num1 / num2)
            Catch ex As DivideByZeroException
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
            End Try
            output = value
        Case "*"
            output = num1 * num2
        Case "Mod"
            output = (num1 Mod num2)
        Case "^"
            output = CDec(Math.Pow(num1, num2))


    End Select
    Return output

End Function
Private Sub isFirst()
    If isFirstExist = False Then
        firstNum = CType(txtCalc.Text, Decimal)
        isFirstExist = True
        txtCalc.Text = "0"
    End If
End Sub

【问题讨论】:

  • 为什么不直接使用 + 按钮,就像使用数字按钮一样(即 make + 键盘加速键)?
  • 我做了,但是加法操作没有得到执行

标签: vb.net calculator


【解决方案1】:

假设您使用文本框来保存用户输入,在 Textchanged 甚至 KeyPressed 处理程序中捕获操作符键并运行函数而不是显示操作符。

类似这样的:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCalc.KeyPress
    If e.KeyChar = "+"c Then
        inputOperator = "+"
        isFirst()
        e.Handled = True
    End If
End Sub

显示操作符设置为假。

【讨论】:

  • 是的,我正在使用文本框来保存用户输入。您能否解释一下“在 Textchanged 甚至 KeyPressed 处理程序中捕获操作员键并运行该功能而不是显示它”的意思。
  • @sam_rox 我为你添加了一个示例。
  • 谢谢。它有效。我不知道有按键事件。我仍然是 vb.net 的初学者
  • @sam_rox - 如果这回答了您的问题,请记住将其标记为已回答。谢谢。
猜你喜欢
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
相关资源
最近更新 更多