当前位置:文档之家› 简单计算器VB完美教程

简单计算器VB完美教程

简单计算器VB完美教程
简单计算器VB完美教程

简单计算器完美教程

方法及步骤:

第一步:在窗口中放置控件。要点:十个数字按钮为按钮数组(画一个,其余几个拷贝出来),四个运算符为按钮数组,其它控件及按钮单独建立(一个一个的画出来)。

第二步:配置各个控件的属性,调整大小和位置

第三步:编写代码

备注:代码可以直接从本文中拷贝过去,但不是太建议这样子做,自己编写的时候可以学到更多的知识。

窗口

属性

代码

Dim s1 As Single, s2 As Single, ysf As String, cfs As Single

Private Sub Command1_Click(Index As Integer)

If cfs > 0 Then '输入数字之前判断是否为重复计算(cfs重复算)

Text1.Text = "" '重复算就清零之前的显示

cfs = 0 '重复算代码清零

End If

Text1.Text = Text1.Text & Command1(Index).Caption '数字框按钮与文本框关联

If Len(Text1.Text) > 13 Then '限制录入13位数字

MsgBox "数字不能大于13位"

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)

End If

End Sub

Private Sub Command2_Click(Index As Integer)

s1 = Text1.Text '按运算符时存数据1

ysf = Command2(Index).Caption '提取运算符

Text1.Text = "" '清除文本框里的数据1,等待录入数据2 End Sub

Private Sub Command3_Click()

Text1.Text = Text1.Text + "."

If InStr(Text1.Text, ".") = 1 Then '小数点录入的时候不能是第一位Text1.Text = ""

End If

If InStr(Text1.Text, ".") < Len(Text1.Text) Then '小数点出现的位置只有等于字长才让录入Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '出现第二个小数点会删除

End If

End Sub

Private Sub Command4_Click()

If Text1.Text = "" Then

Exit Sub

End If

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '删除最后一位数

End Sub

Private Sub Command5_Click()

cfs = cfs + 1 '按“=”号后重复算代码加1

s2 = Val(Text1.Text) '提取数据2

Select Case ysf '根据运算符进行运算

Case "+"

Text1.Text = s1 + s2

Case "-"

Text1.Text = s1 - s2

Case "*"

Text1.Text = s1 * s2

Case "/"

If s2 = 0 Then '避免除以零

MsgBox "分母不能为零"

Text1.Text = ""

Else

Text1.Text = s1 / s2

End If

End Select

If Left(Text1.Text, 1) = "." Then

Text1.Text = "o" & Text1.Text

End If

End Sub

Private Sub Command6_Click()

Text1.Text = ""

End Sub

全文完

相关主题
文本预览
相关文档 最新文档