vb计算器代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vb计算器代码相关的知识,希望对你有一定的参考价值。
我设计好的计算器代码(部分),在执行时文本框1,2都会同时执行命令.求解法......
Private Sub Command1_Click()
Text1.SetFocus
Text1.Text = Text1.Text + "1"
Text1.s
Text2.Text = Text2.Text + "1"
End Sub
Private Sub Command10_Click()
Label2.Caption = "/"
End Sub
Private Sub Command11_Click()
Label2.Caption = "*"
End Sub
Private Sub Command12_Click()
Label2.Caption = "-"
End Sub
Private Sub Command13_Click()
Label2.Caption = "+"
End Sub
Private Sub Command2_Click()
Text1.Text = Text1.Text + "2"
Text2.Text = Text2.Text + "2"
End Sub
Private Sub Command3_Click()
Text1.Text = Text1.Text + "3"
Text2.Text = Text2.Text + "3"
End Sub
Private Sub Command4_Click()
Text1.Text = Text1.Text + "4"
Text2.Text = Text2.Text + "4"
End Sub
Private Sub Command5_Click()
Text1.Text = Text1.Text + "5"
Text2.Text = Text2.Text + "5"
End Sub
Private Sub Command6_Click()
Text1.Text = Text1.Text + "6"
Text2.Text = Text2.Text + "6"
End Sub
Private Sub Command7_Click()
Text1.Text = Text1.Text + "7"
Text2.Text = Text2.Text + "7"
End Sub
Private Sub Command8_Click()
Text1.Text = Text1.Text + "8"
Text2.Text = Text2.Text + "8"
End Sub
Private Sub Command9_Click()
Text1.Text = Text1.Text + "9"
Text2.Text = Text2.Text + "9"
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Dim a, b, c, d
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Label2.Caption
d = Val(Text3.Text)
If c = "+" Then
d = a + b
End If
If c = "-" Then
d = a - b
End If
If c = "*" Then
d = a * b
End If
If c = "/" Then
d = a / b
End If
End Sub
Text1 BackColor=&H00C0FFFF& Text=“0.”
Command1 Index 0――16
Index 0――9 Caption=“0”-“9” Index 10 Caption=“.”
Index 11 Caption=“=”
Index 12 Caption=“+”
Index 13 Caption=“-”
Index 14 Caption=“×”
Index 15 Caption=“÷”
Index 16 Caption=“清除”
如果想改变一些其他的属性,比如字体,可以自己定义一下Font属性,如果相对多个控件的共同属性进行改变,可以在窗体上按下鼠标,拖动,把多个控件一起选定,然后对它们的共同属性进行修改。
别看这个东西小,还费了近我两个小时呢,不觉大叹手生,里面也有些地方很绕弯子,我尽量解释清楚。
在编写程序之前,我们先来回顾一下计算器的使用。
按下数字键在液晶屏上会出现数字,这是最基本的功能。
第一次按下运算键,再按下数字键,将重新输入第二个数字,再次按下运算键,将算出前两个数的结果,然后按下数字键,将又重新输入新的数字。
输入数字后,按下“等于键”将仍输出原来的数字,输入运算符和第二个数字后在按下“等于”,将得到二者的结果。然后又重复第二、三步。
按下“清除”,数据被全部清除。
了解了这些,我们一步一步实现。 先实现数据的输入,请看代码。 与未创立组的控件相比,它的事件里多了Index参数,组里所有的对象都使用同一个事件。 先在General里声明窗体级全局变量:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
因为Text1里是存放字符串的,所以要把输入的数据进行运算,必须把字符型变量转变为实型变量,分别用Num1、Num2和StrNum1、StrNum2进行存放。 在Form_Load事件里给变量初赋值:
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
因为17个按钮使用的是同一事件,所以我们用一个Select Case语句区分各个按钮,双击任意按钮输入代码:
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)’实型转字符型
FirstNum = False’初赋值为假
Else’非初赋值
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1’显示
<还有其他语句,等待插入>
End Select
End Sub
运行程序的话已经可以输入数据,下面我们来处理小数点。 在General里声明新的变量:
Dim PointFlag As Boolean '判断是否已有小数点
我们知道小数点只能输入一次,一个数据中不能有多个小数点存在,所以需要一个布尔变量做判断。
在Form_Load里进行初赋值:
PointFlag = False
在Command的单击事件里接着上面输入新的代码:
Case 10
If Not PointFlag Then’如果没有小数点
If FirstNum Then’如果是第一个字符
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else’已有小数点
Exit Sub’则退出单击事件
End If
PointFlag = True
Text1.Text = StrNum1
下面会复杂一些,我们先跳过等号,而来处理四则运算:
先在General里声明新变量:
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
然后在Form_Load中进行初赋值:
Runsign = 0
SignFlag = False
在接下的Command1_Click中继续输入代码:
Case 12 To 15
FirstNum = True’使下一次可以输入新数据
PointFlag = False
'还原标记值
If SignFlag Then’前面已有运算符未运算
Call Run
Else
SignFlag = True
StrNum2 = StrNum1’把字符串1复制给字符串2
StrNum1 = ""’字符串1清空
End If
Runsign = Index - 11 ‘储存键入的运算符
里面运算的部分我用了一个过程,是因为在按下等号时还会调用到运算过程,这样可以减少代码的输入,也使程序便于浏览。建立过程(Procedure),可以在General部分直接输入,也可以通过菜单里的“Add Procedure”添加。Run的代码如下:
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1’加
equal = Num1 + Num2
Case 2’减
equal = Num1 - Num2
Case 3’乘
equal = Num1 * Num2
Case 4’除
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
下面是其余的代码:(在Command1_Click事件里,接着写)
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else’清除按钮
Call ClearData
里面又用到了一个ClearData过程,实际上就是Form_Load里的变量初赋值,代码如下:
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
这样完整的程序就完成了,本章介绍了VB的第一个控件――按钮,并且举例,希望你继续看下一章――菜单。
下面给出完整的程序代码:
Dim Num1, Num2 As Single
Dim StrNum1, StrNum2 As String
Dim FirstNum As Boolean '判断是否是数字开头
Dim PointFlag As Boolean '判断是否已有小数点
Dim Runsign As Integer '储存运算符号
Dim SignFlag As Boolean '判断是否已有运算符号
Sub ClearData()
Num1 = 0
Num2 = 0
StrNum1 = ""
StrNum2 = ""
FirstNum = True
PointFlag = False
Runsign = 0
SignFlag = False
Text1.Text = "0."
End Sub
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case 1
equal = Num1 + Num2
Case 2
equal = Num1 - Num2
Case 3
equal = Num1 * Num2
Case 4
equal = Num1 / Num2
End Select
StrNum2 = Str(equal)
StrNum1 = StrNum2
Text1.Text = StrNum2
End Sub
Private Sub about_Click()
MsgBox "Create by 周晓阳。1998/7/27", vbDefaultButton1, "VB教程第四章范例"
End Sub
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0 To 9
If FirstNum Then
StrNum1 = Str(Index)
FirstNum = False
Else
StrNum1 = StrNum1 + Str(Index)
End If
Text1.Text = StrNum1
Case 10
If Not PointFlag Then
If FirstNum Then
StrNum1 = "0."
FirstNum = False
Else
StrNum1 = StrNum1 + "."
End If
Else
Exit Sub
End If
PointFlag = True
Text1.Text = StrNum1
Case 12 To 15
FirstNum = True
PointFlag = False
'还原标记值
If SignFlag Then
Call Run
Else
SignFlag = True
StrNum2 = StrNum1
StrNum1 = ""
End If
Runsign = Index - 11
Case 11
If Not SignFlag Then
Text1.Text = StrNum1
equal = Val(StrNum1)
FirstNum = True
PointFlag = False
Else
Call Run
SignFlag = False
End If
Case Else
Call ClearData
End Select
End Sub
Private Sub Form_Load()
Call ClearData
End Sub
按钮还有两个重要的属性Default(默认属性)和Cancel(取消属性),两个属性都以布尔赋值,把Default设为True,则在按下回车键时,不论焦点处于何处,都会触发该按钮的Click事件,而当Cancel属性被设为True,在按下ESC键时,该按钮的Click属性发生。下面举个例子:
建立一个新窗体和两个按钮,Name属性分别为cmdOK和cmdCancel,把前者的Default设为True,后者的Cancel设为True,Caption属性分别为OK和Cancel。
Private Sub cmdCancel_Click()
MsgBox "Cancel按钮被按下", vbDefaultButton1, "提示"
End Sub
Private Sub cmdOK_Click()
MsgBox "OK按钮被按下", vbDefaultButton1, "提示"
End Sub
快捷键大家一定都知道,在VB如何实现呢。其实很简单,在刚才的例子里在建立一个按钮Command1,把它的Caption属性设为Click &ME,如图4-11,在M的下面有一条下划线,运行程序,当你按下Alt+M的时候就触发了Command1_Click事件:
Private Sub Command1_Click()
MsgBox "Click ME 按钮被按下", vbDefaultButton1, "提示"
End Sub
你要是看不懂进这个网站,下半部分就是讲造计算器的:http://www.bianceng.cn/VBjc/vbrm/rm5.htm 参考技术A 从新帮你写一个
Option Explicit
Private StoredValue As Double
Private Const opNone = 0
Private Const opAdd = 1
Private Const opSubtract = 2
Private Const opMultiply = 3
Private Const opDivide = 4
Private Operator As Integer
Private NewEntry As Boolean
'删除最后的字符
Private Sub DeleteCharacter()
Dim txt As String
Dim min_len As Integer
txt = txtDisplay.Text
If Left$(txt, 1) = "-" Then
min_len = 2
Else
min_len = 1
End If
If Len(txt) > min_len Then
txtDisplay.Text = Left$(txt, Len(txt) - 1)
Else
txtDisplay.Text = "0"
End If
End Sub
'清除显示内容,保存运算符
Private Sub cmdClear_Click()
cmdClearEntry_Click
StoredValue = 0
Operator = opNone
End Sub
'清除显示内容
Private Sub cmdClearEntry_Click()
txtDisplay.Text = ""
End Sub
' 显示小数点
Private Sub cmdDecimal_Click()
If InStr(txtDisplay.Text, ".") Then
Beep
Else
If NewEntry Then
txtDisplay.Text = "."
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & "."
End If
End If
End Sub
'计算上一操作符的运算结果
Private Sub cmdEquals_Click()
Dim new_value As Double
If txtDisplay.Text = "" Then
new_value = 0
Else
new_value = CDbl(txtDisplay.Text)
End If
Select Case Operator
Case opNone
StoredValue = new_value
Case opAdd
StoredValue = StoredValue + new_value
Case opSubtract
StoredValue = StoredValue - new_value
Case opMultiply
StoredValue = StoredValue * new_value
Case opDivide
StoredValue = StoredValue / new_value
End Select
Operator = opNone
NewEntry = True
txtDisplay.Text = Format$(StoredValue)
End Sub
' 显示数字
Private Sub cmdNumber_Click(Index As Integer)
If NewEntry Then
txtDisplay.Text = Format$(Index)
NewEntry = False
Else
txtDisplay.Text = txtDisplay.Text & Format$(Index)
End If
End Sub
Private Sub cmdOperator_Click(Index As Integer)
cmdEquals_Click
Operator = Index
NewEntry = True
End Sub
'改变符号
Private Sub cmdPlusMinus_Click()
If NewEntry Then
txtDisplay.Text = "-"
ElseIf Left$(txtDisplay.Text, 1) = "-" Then
txtDisplay.Text = Right$(txtDisplay.Text, 2)
Else
txtDisplay.Text = "-" & txtDisplay.Text
End If
End Sub
'处理键盘按键
Private Sub Form_KeyPress(KeyAscii As Integer)
txtDisplay_KeyPress KeyAscii
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
txtDisplay_KeyUp KeyCode, Shift
End Sub
Private Sub txtDisplay_Change()
txtDisplay.SelStart = Len(txtDisplay.Text)
End Sub
Private Sub txtDisplay_GotFocus()
txtDisplay_Change
End Sub
Private Sub txtDisplay_KeyPress(KeyAscii As Integer)
Dim ch As String
ch = Chr$(KeyAscii)
Select Case ch
Case "0"
cmdNumber_Click 0
Case "1"
cmdNumber_Click 1
Case "2"
cmdNumber_Click 2
Case "3"
cmdNumber_Click 3
Case "4"
cmdNumber_Click 4
Case "5"
cmdNumber_Click 5
Case "6"
cmdNumber_Click 6
Case "7"
cmdNumber_Click 7
Case "8"
cmdNumber_Click 8
Case "9"
cmdNumber_Click 9
Case "*", "x", "X"
cmdOperator_Click opMultiply
Case "+"
cmdOperator_Click opAdd
Case vbCrLf, vbCr, "="
cmdEquals_Click
Case "-"
cmdOperator_Click opSubtract
Case "."
cmdDecimal_Click
Case "/"
cmdOperator_Click opDivide
Case "C", "c"
cmdClearEntry_Click
End Select
KeyAscii = 0
End Sub
Private Sub txtDisplay_KeyUp(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyNumpad0
cmdNumber_Click 0
Case vbKeyNumpad1
cmdNumber_Click 1
Case vbKeyNumpad2
cmdNumber_Click 2
Case vbKeyNumpad3
cmdNumber_Click 3
Case vbKeyNumpad4
cmdNumber_Click 4
Case vbKeyNumpad5
cmdNumber_Click 5
Case vbKeyNumpad6
cmdNumber_Click 6
Case vbKeyNumpad7
cmdNumber_Click 7
Case vbKeyNumpad8
cmdNumber_Click 8
Case vbKeyNumpad9
cmdNumber_Click 9
Case vbKeyMultiply
cmdOperator_Click opMultiply
Case vbKeyAdd
cmdOperator_Click opAdd
Case vbKeySeparator
cmdEquals_Click
Case vbKeySubtract
cmdOperator_Click opSubtract
Case vbKeyDecimal
cmdDecimal_Click
Case vbKeyDivide
cmdOperator_Click opDivide
Case vbKeyBack, vbKeyDelete
DeleteCharacter
End Select
KeyCode = 0
End Sub
以上是关于vb计算器代码的主要内容,如果未能解决你的问题,请参考以下文章