今天给各位分享vb设计一个简易计算器的知识,其中也会对vb设计一个简单的计算器进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
如何用VB做一个简易计算器
分类: 电脑/网络 程序设计 其他编程语言
解析:
我自己写的(VB6.0),可以运行
现在还有一个小问题,就是当其他按钮拥有焦点时,按Enter不起等号的作用
Option Explicit
Dim ClearResult As Boolean '状态
Dim Operand As Double '操作数
Dim Operator As String '运算符
Private Sub CmdClear_Click() '清除结果
ClearResult = False
Operand = 0
txtResult.Text = "0"
CmdEqual.SetFocus
End Sub
Private Sub CmdDot_Click() '小数点
If ClearResult = True Then
txtResult.Text = "0"
ClearResult = False
End If
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
If InStr(1, txtResult.Text, ".") Then
Exit Sub
Else
txtResult.Text = txtResult.Text + "."
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdEqual_Click() '等号
On Error GoTo ErrHand:
If ClearResult = False Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
ClearResult = True
Operand = 0
Operator = ""
CmdEqual.SetFocus
Exit Sub
ErrHand:
MsgBox "操作发生错误:" vbCrLf Err.Description, vbOKOnly + vbCritical, "计算器出错"
ClearResult = True
Operand = 0
Operator = ""
CmdEqual.SetFocus
End Sub
Private Sub CmdNum_Click(Index As Integer) '数字按钮
If ClearResult = True Then
txtResult.Text = ""
ClearResult = False
End If
If txtResult.Text = "0" Then
txtResult.Text = CmdNum(Index).Caption
Else
txtResult.Text = txtResult.Text + CmdNum(Index).Caption
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdOperator_Click(Index As Integer) '运算符按钮
If ClearResult = False And Operator "" Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
Operand = Val(txtResult.Text)
Operator = CmdOperator(Index).Caption
ClearResult = True
CmdEqual.SetFocus
End Sub
Private Sub CmdRecip_Click() '求倒数
If ClearResult = False Then
Select Case Operator
Case "+"
txtResult = Operand + Val(txtResult)
Case "-"
txtResult = Operand - Val(txtResult)
Case "*"
txtResult = Operand * Val(txtResult)
Case "/"
If txtResult "0" Then
txtResult = Operand / Val(txtResult)
Else
txtResult.Text = "除数不能为零。"
ClearResult = True
Operand = 0
End If
End Select
End If
If Val(txtResult.Text) 0 Then
txtResult = 1 / Val(txtResult.Text)
Else
txtResult.Text = "除数不能为零。"
End If
ClearResult = True
CmdEqual.SetFocus
End Sub
Private Sub CmdSign_Click() '正负号
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
If ClearResult = False Or Operator = "" Then
txtResult.Text = -txtResult.Text
Else
txtResult.Text = "0"
End If
CmdEqual.SetFocus
End Sub
Private Sub CmdSqrt_Click() '求平方根
If txtResult.Text = "除数不能为零。" Then
Exit Sub
End If
txtResult = Math.Sqr(Val(txtResult.Text))
ClearResult = True
CmdEqual.SetFocus
End Sub
'键盘按键,包括数字,运算符,小数点,等号
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 42
CmdOperator_Click (0)
Case 43
CmdOperator_Click (2)
Case 45
CmdOperator_Click (3)
Case 47
CmdOperator_Click (1)
Case 46
CmdDot_Click
Case 48
CmdNum_Click (0)
Case 49
CmdNum_Click (1)
Case 50
CmdNum_Click (2)
Case 51
CmdNum_Click (3)
Case 52
CmdNum_Click (4)
Case 53
CmdNum_Click (5)
Case 54
CmdNum_Click (6)
Case 55
CmdNum_Click (7)
Case 56
CmdNum_Click (8)
Case 57
CmdNum_Click (9)
Case 13
CmdEqual_Click
End Select
End Sub
Private Sub Form_Load() '程序启动时加载窗体
If App.PrevInstance Then '只允许运行一个实例
MsgBox "已经有一个本程序的实例在运行!", vbOKOnly + vbInformation, "提示"
Unload Me
Exit Sub
End If
ClearResult = True
End Sub
用vb语言编写一个简易计算器
Dim N1 As Single
Dim ysf As String
Dim qing As Boolean
Dim M As Single
Private Sub C_Click(Index As Integer)
If qing = True Then
L.Caption = ""
qing = False
End If
L.Caption = L.Caption C(Index).Caption
End Sub
Private Sub Command1_Click()
qing = True
Dim N2 As Single
Dim T As Single
N2 = Val(L.Caption)
Select Case ysf
Case "+"
T = N1 + N2
Case "-"
T = N1 - N2
Case "*"
T = N1 * N2
Case "/"
T = N1 / N2
End Select
L.Caption = T
End Sub
Private Sub Command10_Click()
M = Val(L.Caption)
L1.Caption = "M"
End Sub
Private Sub Command11_Click()
M = M + Val(L.Caption)
End Sub
Private Sub Command2_Click()
L.Caption = -Val(L.Caption)
End Sub
Private Sub Command3_Click()
If L.Caption "" Then
L.Caption = Left(L.Caption, Len(L.Caption) - 1)
End If
End Sub
Private Sub Command4_Click()
N1 = 0
L.Caption = ""
ysf = "+"
End Sub
Private Sub Command5_Click()
L.Caption = ""
End Sub
Private Sub Command6_Click()
L.Caption = Sqr(Val(L.Caption))
qing = True
End Sub
Private Sub Command7_Click()
L.Caption = 1 / (Val(L.Caption))
End Sub
Private Sub Command8_Click()
M = 0
L1.Caption = ""
End Sub
Private Sub Command9_Click()
L.Caption = M
End Sub
Private Sub Form_Load()
ysf = "+"
End Sub
Private Sub s_Click(Index As Integer)
N1 = L.Caption
ysf = s(Index).Caption
qing = True
End Sub
Private Sub xsd_Click()
If qing = True Then
L.Caption = ""
qing = False
End If
If InStr(1, L.Caption, ".") = 0 Then
L.Caption = L.Caption "."
End If
End Sub
用VB做一个简单的加减乘除的计算器,代码是什么?
我们现在首先要做的就是拉出Label,准备三个Label,把label1,label2,label3,这些改成第一个数、第二个数、结果。
我们现在要text也要拉出三个,把了text里面text1、text2、text3全部删掉。
我们现在要把command拉出四个,把里面command1-4都给成+-*/
现在我们现在要双击+,我们开始进行输入代码
在里面我们现在要:text3.text=val(text1.text)+val(text2.text)
我们要把这个(text3.text=val(text1.text)+val(text2.text))复制,把里面+号分别改成-*/
如图,就像这样就可以了,可以行径了,如果你在哪个代码里弄错了,你看一下还要哪些没有拉出来,或者你哪个代码输错了。
感谢观看。
如何用VB做简易的四则运算计算器
很简单的:窗口放入四个COMMAND控件(Caption属性分别为+、-、X、÷),三个TEXT控件,再放入三个Label控件(Caption属性分别为:操作数1、操作数2和计算结果),窗口的Caption属性改为“四则运算”
代码如下:
Private Sub Command1_Click()
If Val(Text1.Text) + Val(Text2.Text) Int(Val(Text1.Text) + Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) + Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
End If
End Sub
Private Sub Command2_Click()
If Val(Text1.Text) - Val(Text2.Text) Int(Val(Text1.Text) - Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) - Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
End If
End Sub
Private Sub Command3_Click()
If Val(Text1.Text) * Val(Text2.Text) Int(Val(Text1.Text) * Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) * Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
End If
End Sub
Private Sub Command4_Click()
If Val(Text1.Text) / Val(Text2.Text) Int(Val(Text1.Text) / Val(Text2.Text)) Then
Text3.Text = Format(Val(Text1.Text) / Val(Text2.Text), "0.######")
Else
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
End If
End Sub
关于vb设计一个简易计算器和vb设计一个简单的计算器的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
2、本站永久网址:https://www.yuanmacun.com
3、本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
4、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
6、本站资源大多存储在云盘,如发现链接失效,请联系我们我们会第一时间更新。
源码村资源网 » vb设计一个简易计算器(vb设计一个简单的计算器)
1 评论