1、在电脑桌面打开软件“Visual Basic”,新建一个文件
2、放置3个“Text”部件和4个“Command”部件,调整好位置;
3、对这7个小部件进行修饰,更改command部件的名称为“+、-、*、/”方便识别,清除“Test”部件里的内容;
4、双击“Command”部件进入编辑页面,输入图中所示的命令;
5、命令输完以后,关闭编辑页面,运转程序,在前两个方框上输出数字,只要点击下方的“+、-、*、/、”四个部件,第三个方框就会表现成果,实现加减乘数运算。
Private Sub Command1_Click()'文本框Text3的内容等于Text1的数值减去Text2的数值
'其中Val 函数用来返回字串表达式的数值
Me.Text3.Text = Val(Me.Text1.Text) - Val(Me.Text2.Text)
End Sub
创建text1、text2、command1、command2、command3、command4代码如下Private Sub Form_Load()Text1.Text = ""
Text2.Text = ""
Command1.Caption = "+"
Command2.Caption = "-"
Command3.Caption = "*"
Command4.Caption = "/"
End Sub
Private Sub Command1_Click()
s = Val(Text1.Text) + Val(Text2.Text)
MsgBox Text1.Text &" + " &Text2.Text &" = " &Str(s)
End SubPrivate Sub Command2_Click()
s = Val(Text1.Text) - Val(Text2.Text)
MsgBox Text1.Text &" - " &Text2.Text &" = " &Str(s)
End SubPrivate Sub Command3_Click()
s = Val(Text1.Text) * Val(Text2.Text)
MsgBox Text1.Text &" * " &Text2.Text &" = " &Str(s)
End SubPrivate Sub Command4_Click()
s = Val(Text1.Text) / Val(Text2.Text)
MsgBox Text1.Text &" / " &Text2.Text &" = " &Str(s)
End Sub
Private Sub Text1_lostfocus()
If Text1.Text = "" Then MsgBox "输入第一个数值"
End SubPrivate Sub Text2_lostfocus()
If Text2.Text = "" Then MsgBox "输入第二个数值"
If Text2.Text = "0" Then
MsgBox "除数不能为0"
Text2.SetFocus
End If
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)