由于最近公司需要用到VB6.0.所以自己又学习了一遍。语法真的真的和C#差别很大。好吧,先上一个简单的DEMO,人人都可以看懂的计算器。
❶ 拖控件,然后设置界面
❷ 定义变量,存值
Dim num1, num2, num3, flg As String '定义单个变量,存值
❸ 设置按钮事件
Private Sub bt0_Click(Index As Integer)
If flg = "" Then
num1 = num1 + "0"
lbresult.Caption = num1
ElseIf num3 <> "" Then
num1 = num1 + "0"
lbresult.Caption = Str(num3) + flg + num1
Else
num2 = num2 + "0"
lbresult.Caption = num1 + flg + num2
End If
End Sub
❹ 设置符号事件
If num3 = "" Then
flg = "+"
lbresult.Caption = num1 + flg
Else
flg = "+"
lbresult.Caption = Str(num3) + flg
End If
❺ 设置等号事件
If num3 = "" Then
Select Case flg
Case "+":
num3 = CDbl(num1) + CDbl(num2)
lbresult.Caption = num3
Case "-":
num3 = CDbl(num1) - CDbl(num2)
lbresult.Caption = num3
Case "*":
num3 = CDbl(num1) * CDbl(num2)
lbresult.Caption = num3
Case "/":
num3 = CDbl(num1) / CDbl(num2)
lbresult.Caption = num3
End Select
Else
Select Case flg
Case "+":
num3 = Str(CDbl(num3) + CDbl(num1))
lbresult.Caption = Str(num3)
Case "-":
num3 = Str(CDbl(num3) - CDbl(num1))
lbresult.Caption = Str(num3)
Case "*":
num3 = Str(CDbl(num3) * CDbl(num1))
lbresult.Caption = Str(num3)
Case "/":
num3 = Str(CDbl(num3) / CDbl(num1))
lbresult.Caption = Str(num3)
End Select
End If
num1 = ""
num2 = ""
flg = ""
End Sub
❻ 结果