winforms 制作一个简单的计算器,但它只会加法

hmae6n7t  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(113)

我的问题是,每次我试图在我的相等按钮中编码减法,如TextBox1.Text = firstValue - secondValue,加法按钮也会变成减法,那么我该如何解决这个问题呢?

Dim firstValue, secondValue As Int32

Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
    secondValue = TextBox1.Text
    TextBox1.Text = firstValue + secondValue
    (here's the equal button)
End Sub

Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
    TextBox1.Clear()
End Sub

Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
    firstValue = Convert.ToInt32(TextBox1.Text)
    TextBox1.Clear()
    (here's the Addition Button )
End Sub

Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
    (Here's the button of the Subtraction )
End Sub

字符串

zaqlnxep

zaqlnxep1#

这是接近Windows的计算器的基本功能,但有很多工作要做,以获得其全部功能。对于一个简单的计算器来说,它已经足够好了

Private firstOperand As Double
Private func As Func(Of Double, Double, Double)
Private Sub DivideButton_Click(sender As Object, e As EventArgs) Handles DivideButton.Click
    If Double.TryParse(OperandTextBox.Text, firstOperand) Then
        func = Function(a, b) a / b
        ExpressionLabel.Text = $"{firstOperand} /"
    End If
End Sub
Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) Handles MultiplyButton.Click
    If Double.TryParse(OperandTextBox.Text, firstOperand) Then
        func = Function(a, b) a * b
        ExpressionLabel.Text = $"{firstOperand} x"
    End If
End Sub
Private Sub SubtractButton_Click(sender As Object, e As EventArgs) Handles SubtractButton.Click
    If Double.TryParse(OperandTextBox.Text, firstOperand) Then
        func = Function(a, b) a - b
        ExpressionLabel.Text = $"{firstOperand} -"
    End If
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
    If Double.TryParse(OperandTextBox.Text, firstOperand) Then
        func = Function(a, b) a + b
        ExpressionLabel.Text = $"{firstOperand} +"
    End If
End Sub
Private Sub EqualsButton_Click(sender As Object, e As EventArgs) Handles EqualsButton.Click
    Dim secondOperand As Double
    If Double.TryParse(OperandTextBox.Text, secondOperand) Then
        ExpressionLabel.Text &= $" {secondOperand}"
        OperandTextBox.Text = $"{func(firstOperand, secondOperand)}"
    End If
End Sub

字符串
您将需要添加控件。使用好的名称总是有帮助的,因为当Button11可以被称为AddButton时,没有人想猜测Button11做什么。
单击按钮时将操作存储在函数中,然后在按下equals时调用该函数。


的数据

lzfw57am

lzfw57am2#

以下是允许连续计算的解决方案:

Private Enum Operation
    None
    Add
    Subtract
End Enum

Dim accumulator As Integer
Dim op As Operation

Private Sub AdditionButton_Click(sender As Object, e As EventArgs) Handles additionButton.Click
    Execute()
    op = Operation.Add
End Sub

Private Sub SubtractionButton_Click(sender As Object, e As EventArgs) Handles subtractionButton.Click
    Execute()
    op = Operation.Subtract
End Sub

Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles clearButton.Click
    op = Operation.None
    accumulator = 0
    resultTextBox.Text = "0"
End Sub

Private Sub EqualsButton_Click(sender As Object, e As EventArgs) Handles equalsButton.Click
    Execute()
    op = Operation.None
End Sub

Private Sub Execute()
    Select Case op
        Case Operation.None
            If IsNumeric(inputTextBox.Text) Then
                accumulator = CInt(inputTextBox.Text)
            End If
        Case Operation.Add
            Dim secondValue As Integer = CInt(inputTextBox.Text)
            accumulator += secondValue
        Case Operation.Subtract
            Dim secondValue As Integer = CInt(inputTextBox.Text)
            accumulator -= secondValue
    End Select
    resultTextBox.Text = accumulator.ToString()
    inputTextBox.Text = ""
    inputTextBox.Select()
End Sub

字符串
我使用Enum来记住单击equal按钮时将执行的操作。您也可以再次单击+或-,输入新的数字,而不是单击等于。只有在输入最后一个数字后,才必须单击等于按钮。
清除按钮并不是严格要求的,但很高兴有。
注意我有两个文本框。一个用于输入,一个用于显示结果。结果文本框有Enabled = FalseBackColor = White。我还为它选择了一个更大的字体。

相关问题