用VB做一个简单的加减乘除的计算器,代码是什么

用VB做一个简单的加减乘除的计算器,代码是什么,第1张

我们现在首先要做的就是拉出Label,准备三个Label,把label1,label2,label3,这些改成第一个数、第二个数、结果。

我们现在要text也要拉出三个,把了text里面text1、text2、text3全部删掉。

我们现在要把command拉出四个,把里面command1-4都给成+-/

现在我们现在要双击+,我们开始进行输入代码

在里面我们现在要:text3text=val(text1text)+val(text2text)

我们要把这个(text3text=val(text1text)+val(text2text))复制,把里面+号分别改成-/

如图,就像这样就可以了,可以行径了,如果你在哪个代码里弄错了,你看一下还要哪些没有拉出来,或者你哪个代码输错了。

感谢观看。

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)

Text1Text = Text1Text & Command1(Index)Caption ’将command1的单击事件与文本框显示的内容连接

End Sub

Private Sub Command2_Click()

Text1Text = Text1Text + “。”

If (InStr(Text1Text, “。”) = 1) Then ‘第一位不能为小数

Text1Text = “”

End If

If InStr(Text1Text, “。”) 《 Len(Text1Text) Then ’防止出现两个小数点

Text1Text = Left

(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub

Command3_Click()

s2 = Val(Text1Text) ‘开始加减乘除运算

Select Case ysf Case “+”

Text1Text = s1 + s2

Case “-”

Text1Text = s1 - s2

Case “”

Text1Text = s1 s2

Case “/”

If s2 = 0 Then

MsgBox “分母不能为零!”

Text1Text = “”

Else

Text1Text = s1 / s2 End If End Select

Text1 = IIf(Left(Text1Text, 1) = “。”, 0 & Text1Text, Text1Text) ‘

这个很关键,如果没有这个的话,得出小于1的小数前面没有0

End Sub

Private Sub Command4_Click()

If Text1Text = “” Then ’文本为空就结束

Exit Sub

End If

Text1Text = Left(Text1Text, Len(Text1Text) - 1) ‘文本退一格

End Sub

Private Sub Command5_Click()

Text1Text = “” ’清除当前框内文本

End Sub

Private Sub Command6_Click(Index As Integer)

s1 = Val(Text1Text) ‘将s1隐藏起来 ysf = Command6(Index)Caption

Text1Text = “”

End Sub

Private Sub Command7_Click()

If Left(Text1Text, 1) 《》 “-” Then ’判断作为负数

Text1Text = “-” & Text1Text

Else

Text1Text = Right(Text1Text, Len(Text1Text) - 1)

End If

End Sub

Private Sub Command8_Click()

Text1Text = Text1Text Text1Text ‘平方

End Sub

Visual Basic(VB)是由微软公司开发的包含环境的事件驱动编程语言。它源自于BASIC编程语言。VB拥有图形用户界面(GUI)和快速应用程序开发(RAD)系统,可以轻易的使用DAO、RDO、ADO连接数据库,或者轻松的创建ActiveX控件。程序员可以轻松地使用VB提供的组件快速创建一个应用程序。

参考链Visual Basic——百度百科接

nMax = Val(ToNum(Combo1Text)) '将下拉列表框Combo1中的文本转变为数字

If nMax < 1 Then nMax = 1 + Int(Rnd Combo3ListCount)

'如果这个数字小于1,则nMax为1+"Combo3条目数" 乘 "0-1间的随机数"这个值

nMax就是上面计算的这个数值

Dim v As Boolean

Dim s As Integer

Dim X As Double

Dim Y As Double

Private Sub Command1_Click(Index As Integer)

If Form1Tag = "T" Then

If Index = 10 Then

Text1Text = "0"

Else

Text1Text = Command1(Index)Caption

End If

Form1Tag = ""

Else

Text1Text = Text1Text & Command1(Index)Caption

End If

End Sub

Private Sub Command2_Click(Index As Integer)

Form1Tag = "T"

If v Then

X = Val(Text1Text)

v = Not v

Else

Y = Val(Text1Text)

Select Case s

Case 0

Text1Text = X + Y

Case 1

Text1Text = X - Y

Case 2

Text1Text = X Y

Case 3

If Y <> 0 Then

Text1Text = X / Y

Else

MsgBox ("不能以0为除数")

Text1Text = X

v = False

End If

Case 4

Y = 0

v = False

End Select

X = Val(Text1Text)

End If

s = Index

End Sub

Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single)

End Sub

控件自己添加吧,空间名要和代码名一致

分析:由于我们不知道输入的这个整数是几位数,所以要先判断整数的位数,然后用循环找出每个位数具体的数值,最后加起来。

先建立2个文本框Text1、Text2,1个按钮Command1。 *** 作:在Text1中输入一个整数,再单击按钮,则结果显示在Text2。下面是Command1的代码:

Private Sub Command1_Click()

Dim i,x,y,a() as integer //i用来做循环,x用来保存整数位数,y用来保存求和结果,a() 数组用来保存每个位数数值。

Text2text="" //程序开始时,先清空Text2。

x=len(Text1text) //先判断输入的整数的位数。

for i=1 to x

a(i)=val(mid(Text1text,i,1)) //用读取字符串函数把各位数找出来,并转化成数字放到数组a(i) 中。

y=y+a(i) //把各位数加起来。

next i

Text2text=y //把求和结果显示在Text2中。

End Sub

有什么问题可以追问。

哇 楼主,你自己都写出写出控件公式了怎么还来提问。。。

Private Sub Command1_Click()

Dim num As Double

If Trim(Text1Text) <> "0" Then

num = (Val(Text2Text) - Val(Text1Text)) / Val(Text1Text)

End If

Select Case num

Case num < 01001

Text3Text = 07

Text4Text = Text2Text 08 + 07 20

Case num >= 01001 And num < 02001

Text3Text = 075

Text4Text = Text2Text 08 + 075 20

Case num >= 02001 And num < 03001, Text3Text = 08

Text4Text = Text2Text 08 + 08 20

Case num >= 03001 And num < 04001, Text3Text = 085

Text4Text = Text2Text 08 + 085 20

Case num >= 04001 And num < 05001, Text3Text = 09

Text4Text = Text2Text 08 + 09 20

Case num >= 05001 And num < 06001, Text3Text = 095

Text4Text = Text2Text 08 + 095 20

Case num >= 06001 And num < 07001, Text3Text = 1

Text4Text = Text2Text 08 + 1 20

Case num >= 07001 And num < 08001, Text3Text = 105

Text4Text = Text2Text 08 + 105 20

Case num >= 0 And num < 08001

Text3Text = 11

Text4Text = Text2Text 08 + 11 20

End Select

End Sub

这个只是第一部分,“如果框2,框4中输入数值(带小数),求框1,框3

点命令按扭,”这段开始后面和前面冲突 恕难实现。。

以上就是关于用VB做一个简单的加减乘除的计算器,代码是什么全部的内容,包括:用VB做一个简单的加减乘除的计算器,代码是什么、用VB编写一个计算器程序的代码、谁能说明一下vb课设算数四则运算的程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10118764.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存