VS2023
创建界面一共使用了2种控件,显示数字的是TextBox,各种按钮是Button。
注意事项TextBox控件的Multiline属性默认为false,如果要修改TextBox控件的大小,就需要将Multiline属性改为True。
按钮事件设置三个全局变量 分别用来记录计算器显示框中的输入数字 到目前为止的计算结果 以及记录所使用的运算符
Dim num As Double = 0 Dim sum As Double = 0 Dim f As String = ""数字按钮以1为例,其余数字代码基本类似
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click num = num * 10 + 1 TextBox1.Text = numEnd Sub运算符号按钮以+为例,其余运算符号代码都相类似
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click If (f = "") Then sum = num ElseIf (f = "+") Then sum = sum + num ElseIf (f = "-") Then sum = sum - num ElseIf (f = "*") Then sum = sum * num ElseIf (f = "/") Then sum = sum / num End If num = 0 f = "+" TextBox1.Clear() End Sub最后等号的按钮事件为
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click If (f = "") Then sum = num ElseIf (f = "+") Then sum = sum + num ElseIf (f = "-") Then sum = sum - num ElseIf (f = "*") Then sum = sum * num ElseIf (f = "/") Then sum = sum / num End If num = 0 f = "" TextBox1.Text = sum sum = 0 End Sub 这样就结束啦。刚刚开始学这个,希望慢慢进步哟。