求用VB编写的简易计算器(只有加减乘除,按ESC键清除)的源码
- 提问者网友:萌卜娃娃
- 2021-04-16 08:12
- 五星知识达人网友:十年萤火照君眠
- 2021-04-16 08:59
- 1楼网友:行雁书
- 2021-04-16 10:37
Option Explicit Dim strNumber As String Dim strPoint As String Dim dblNum1 As Double Dim intOperator As Integer '清除结果 Private Sub cmdGT_Click() txtDisplay.Text = "0." strNumber = "" strPoint = "." intOperator = 7 End Sub '输入数字 Private Sub cmdNumber_Click(Index As Integer) strNumber = strNumber & cmdNumber(Index).Caption txtDisplay.Text = strNumber & strPoint End Sub Private Sub cmdOnOff_Click() End End Sub '运算过程 Private Sub cmdOperator_Click(Index As Integer) Dim dblnum2 As Double '是第一次单击运算符时,将输入的值先赋给第一个数,否则赋值给第二个数进行运算 If intOperator = 7 Then dblNum1 = CDbl(txtDisplay.Text) Else dblnum2 = CDbl(Val(txtDisplay.Text)) '根据输入的符号进行运算 '求普通运算 Select Case intOperator Case 0 dblNum1 = dblNum1 + dblnum2 Case 1 dblNum1 = dblNum1 - dblnum2 Case 2 dblNum1 = dblNum1 * dblnum2 Case 3 If dblnum2 <> 0 Then dblNum1 = dblNum1 / dblnum2 Else MsgBox "除数不能为“0”!请重新输入除数。", vbOKOnly + vbInformation, "除零错误" Index = intOperator End If Case 6 dblNum1 = dblNum1 * dblnum2 / 100 End Select End If '取得当前输入的运算符,以做下次运算 intOperator = Index strNumber = "" txtDisplay = CStr(dblNum1) '判断是否为文本框中的数字加点 If Not txtDisplay Like "*.*" Then txtDisplay.Text = txtDisplay.Text & "." End If End Sub Private Sub cmdPoint_Click() strNumber = strNumber & strPoint strPoint = "" End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) '使被按下的数字键的对应按钮取得焦点 Select Case KeyCode Case 48 To 57 cmdNumber(KeyCode - 48).SetFocus Case 96 To 105 cmdNumber(KeyCode - 96).SetFocus Case Else '使按下的符号键对应的按钮取得焦点 If KeyCode = 107 Or (Shift = vbShiftMask And KeyCode = 187) Then cmdOperator(0).SetFocus cmdOperator_Click (0) ElseIf KeyCode = 109 Or KeyCode = 189 Then cmdOperator(1).SetFocus cmdOperator_Click (1) ElseIf KeyCode = 106 Or (Shift = vbShiftMask And KeyCode = 56) Then cmdOperator(2).SetFocus cmdOperator_Click (2) ElseIf KeyCode =
我有现成的程序,要的话加我,发给你