做一个简易计算器(VB版) | 您所在的位置:网站首页 › vb简单计算器编程代码 › 做一个简易计算器(VB版) |
今天小编来带大家用VB做一个简易计算器
废话不多说,下面就是具体步骤了
1、创建控件组的方法首先创建一个命令按钮,调整其大小(觉得合适就行),名称为Command1,Caption 属性为数字 0 ;然后进行“复制”和“粘贴”,当选择“粘贴”时,出现对话框提示已有一个同名控件,询问是否创建控件组,选择“是”后,即创建了一个名为“Command”的控件组。
这时,第一个按钮的Index属性值默认为“0”,第二个的Index属性值自动设为“1”,并且大小与第一个按钮相同,只需修改其 Caption 属性为数字“1”并将其拖至合适位置即可。此后继续使用“粘贴”的方法建立其他控件组中其余按钮,共20个按钮,每建立一个,就将它拖到合适处,并修改相应的Caption属性值。
2、各控件组其属性设置如下:
Dim s1 As Single, s2 As Single, ysf As String ‘定义两个单精度数变量用与存放参与运算的数,一个字符型存放运算符 Private Sub Command1_Click(Index As Integer) If Text1.Text = “0” Then Text1.Text = Command1(Index).Caption Else Text1.Text = Text1.Text & Command1(Index).Caption '将command1的单击事件与文本框显示的内容连接 End If End Sub Private Sub Command2_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 Command3_Click(Index As Integer) s2 = Val(Text1.Text) '开始加减乘除运算 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 Text1 = IIf(Left(Text1.Text, 1) = “.”, 0 & Text1.Text, Text1.Text) '这个很关键,如果没有这个的话,得出小于1的小数前面没有0 End Sub Private Sub Command4_Click(Index As Integer) If Text1.Text = " " Then '文本为空就结束 Text1.Text = “0” ElseIf Len(Text1.Text) = “1” Then Text1.Text = “0” Else Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '文本退一格 End If End Sub Private Sub Command5_Click(Index As Integer) Text1.Text = " " End Sub Private Sub Command6_Click(Index As Integer) s1 = Val(Text1.Text) '将s1隐藏起来 ysf = Command6(Index).Caption Text1.Text = “” End Sub Private Sub Command7_Click() If Left(Text1.Text, 1) “-” Then '判断作为负数 Text1.Text = “-” & Text1.Text Else Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1) End If End Sub Private Sub Command8_Click(Index As Integer) Text1.Text = Text1.Text * Text1.Text End Sub 输完上述代码后的程序图片如下:
|
CopyRight 2018-2019 实验室设备网 版权所有 |