我正在研究计算器,我想知道是否有人可以提示如何在文本框中显示数字和运算符。我试过自己做但没有成功......
问候,
Pd积。仅提示,而非代码完成。我需要学习!
这是我的代码。
Public Class Form1
Dim FirstNumber As Double
Dim Operation As String
Dim ClearField As Boolean = True
Private Sub AddNumber(NewValue As String)
If ClearField = True Then Viewer.Text = ""
Viewer.Text &= NewValue
ClearField = False
End Sub
'BUTTONS
'Numbers
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddNumber("1")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
AddNumber("2")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
AddNumber("3")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
AddNumber("4")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
AddNumber("5")
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
AddNumber("6")
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
AddNumber("7")
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
AddNumber("8")
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
AddNumber("9")
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Zero.Click
AddNumber("0")
End Sub
'Buttons "," "del" "clear"
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Dot.Click
Viewer.Text = Viewer.Text + ","
End Sub
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Del.Click
Viewer.Text = Viewer.Text.Remove(Viewer.Text.Length - 1, 1)
End Sub
Private Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
Viewer.Text = ""
End Sub
Private Sub Sum_Click(sender As Object, e As EventArgs) Handles Sum.Click
FirstNumber = Val(Viewer.Text)
Operation = "SUM"
Viewer.Text = ""
End Sub
Private Sub Minus_Click(sender As Object, e As EventArgs) Handles Minus.Click
FirstNumber = Val(Viewer.Text)
Operation = "MINUS"
Viewer.Text = ""
End Sub
Private Sub Multi_Click(sender As Object, e As EventArgs) Handles Multi.Click
FirstNumber = Val(Viewer.Text)
Operation = "MULTI"
Viewer.Text = ""
End Sub
Private Sub Div_Click(sender As Object, e As EventArgs) Handles Div.Click
FirstNumber = Val(Viewer.Text)
Operation = "DIV"
Viewer.Text = ""
End Sub
Private Sub Result_Click(sender As Object, e As EventArgs) Handles Result.Click
Dim SecondNumber As Double
Dim Result As Double
SecondNumber = Val(Viewer.Text)
'Fx
If Operation = "SUM" Then
Result = FirstNumber + SecondNumber
End If
If Operation = "MINUS" Then
Result = FirstNumber - SecondNumber
End If
If Operation = "MULTI" Then
Result = FirstNumber * SecondNumber
End If
If Operation = "DIV" Then
Result = FirstNumber / SecondNumber
End If
Viewer.Text = Result
ClearField = True
End Sub
Private Sub SalirToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles SalirToolStripMenuItem1.Click
End
End Sub
结束班
答案 0 :(得分:1)
a)不要使用Val
这很糟糕,从长远来看会造成问题。
b)使用枚举:
Enum myOp
None
Adding
Subbing
Divide
Equals
End Enum
Dim Op As myOp
这些最终具有从0到4的整数值,但您可以按名称引用它们。 Case语句可以减少大多数人认为更容易阅读的代码:
Select Case Op
Case myOp.Adding
Result = FirstNumber + SecondNumber
Case myOp.Subbing
Result = FirstNumber - SecondNumber
' etc
当你忘记它是“添加”,“添加”,“添加”,“添加”还是“添加”(和/或拼错它“添加”)时,这可以防止令人讨厌的错误。