⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 实现计算器功能 加。减。乘。除 可以激活了吧
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "计算器"
   ClientHeight    =   2295
   ClientLeft      =   150
   ClientTop       =   435
   ClientWidth     =   2580
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   2295
   ScaleWidth      =   2580
   StartUpPosition =   2  '屏幕中心
   Begin VB.CommandButton a 
      Caption         =   "Command1"
      Height          =   255
      Left            =   3120
      TabIndex        =   0
      Top             =   1320
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "1"
      Height          =   375
      Index           =   1
      Left            =   120
      TabIndex        =   17
      Top             =   360
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "2"
      Height          =   375
      Index           =   2
      Left            =   720
      TabIndex        =   16
      Top             =   360
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "3"
      Height          =   375
      Index           =   3
      Left            =   1320
      TabIndex        =   15
      Top             =   360
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "4"
      Height          =   375
      Index           =   4
      Left            =   120
      TabIndex        =   14
      Top             =   840
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "5"
      Height          =   375
      Index           =   5
      Left            =   720
      TabIndex        =   13
      Top             =   840
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "6"
      Height          =   375
      Index           =   6
      Left            =   1320
      TabIndex        =   12
      Top             =   840
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "7"
      Height          =   375
      Index           =   7
      Left            =   120
      TabIndex        =   11
      Top             =   1320
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "8"
      Height          =   375
      Index           =   8
      Left            =   720
      TabIndex        =   10
      Top             =   1320
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "9"
      Height          =   375
      Index           =   9
      Left            =   1320
      TabIndex        =   9
      Top             =   1320
      Width           =   495
   End
   Begin VB.CommandButton cmdOK 
      Caption         =   "="
      Height          =   375
      Left            =   1320
      TabIndex        =   2
      Top             =   1800
      Width           =   495
   End
   Begin VB.TextBox Text1 
      Alignment       =   1  'Right Justify
      Height          =   270
      Left            =   120
      Locked          =   -1  'True
      TabIndex        =   1
      Text            =   "0"
      Top             =   0
      Width           =   2295
   End
   Begin VB.CommandButton cmdOper 
      Caption         =   "+"
      Height          =   375
      Index           =   1
      Left            =   1920
      TabIndex        =   8
      Top             =   360
      Width           =   495
   End
   Begin VB.CommandButton cmdOper 
      Caption         =   "-"
      Height          =   375
      Index           =   2
      Left            =   1920
      TabIndex        =   7
      Top             =   840
      Width           =   495
   End
   Begin VB.CommandButton cmdOper 
      Caption         =   "*"
      Height          =   375
      Index           =   3
      Left            =   1920
      TabIndex        =   6
      Top             =   1320
      Width           =   495
   End
   Begin VB.CommandButton cmdOper 
      Caption         =   "/"
      Height          =   375
      Index           =   4
      Left            =   1920
      TabIndex        =   5
      Top             =   1800
      Width           =   495
   End
   Begin VB.CommandButton cmdClear 
      Caption         =   "C"
      Height          =   375
      Left            =   720
      TabIndex        =   4
      Top             =   1800
      Width           =   495
   End
   Begin VB.CommandButton cmdNum 
      Caption         =   "0"
      Height          =   375
      Index           =   0
      Left            =   120
      TabIndex        =   3
      Top             =   1800
      Width           =   495
   End
   Begin VB.Menu mnuEdit 
      Caption         =   "编辑(&E)"
      Begin VB.Menu mnuEditCopy 
         Caption         =   "复制(&C)"
         Shortcut        =   ^C
      End
      Begin VB.Menu mnuEditPaste 
         Caption         =   "粘贴(&P)"
         Shortcut        =   ^V
      End
   End
   Begin VB.Menu mnuView 
      Caption         =   "查看(&V)"
      Begin VB.Menu mnuViewStand 
         Caption         =   "标准型(&T)"
         Checked         =   -1  'True
      End
      Begin VB.Menu mnuViewScience 
         Caption         =   "科学型(&S)"
      End
      Begin VB.Menu mnuViewLine0 
         Caption         =   "-"
      End
      Begin VB.Menu mnuViewSep 
         Caption         =   "数字分组(&I)"
      End
   End
   Begin VB.Menu mnuHelp 
      Caption         =   "帮助(&H)"
      Begin VB.Menu mnuHelpTitle 
         Caption         =   "帮助主题(&H)"
      End
      Begin VB.Menu mnuHelpLine0 
         Caption         =   "-"
      End
      Begin VB.Menu mnuHelpAbout 
         Caption         =   "关于计算器(&A)"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim flag As Integer '表示运算符
Dim isClickNumLast As Boolean '上步操作是否按的是数字
Dim isClickOkLast As Boolean '上步操作是否按的是“=”
Dim s1 As String '前一步的值
Dim s2 As String '当前框中的值

Private Sub Form_Load()
    Call initValue '初始化数据
End Sub
'点数字的操作
Private Sub cmdNum_Click(Index As Integer)
    '如果上一步点的是等于号的话这里就需要将所有变量初始化一下
    If isClickOkLast Then Call initValue
    
    '这里文本框的显示很重要,解释如下:
    '1.如果上步点的是数字那么这次显示的应该是 Text1.Text & Index
    '2.如果上步点的不是数字那么这次显示的应该是 Index
    '3.最后加上val是因为防止出现文本框的第一位显示 0
    Text1.Text = Val(IIf(isClickNumLast, Text1.Text, "") & Index)
    isClickNumLast = True
    isClickOkLast = False
    a.SetFocus
End Sub
'点运算符的操作
Private Sub cmdOper_Click(Index As Integer)
    '这里点按钮的操作也很重要,因为点按钮的同时还要计算出结果并显示出来
    '如果上步点的是数字按钮则开始计算并显示
    If isClickNumLast Then
        Call computeResult
        Text1.Text = s1
    End If
    flag = Index
    isClickNumLast = False
    isClickOkLast = False
End Sub
'按等于号
Private Sub cmdOK_Click()
    Call computeResult
    Text1.Text = s1
    isClickNumLast = False
    isClickOkLast = True
End Sub
'清屏
Private Sub cmdClear_Click() '清屏
    Call initValue
    a.SetFocus
End Sub
'计算结果
Private Sub computeResult()
    If Not isClickOkLast Then s2 = Text1.Text
    Select Case flag 'flag为运算符,分别为+-*/
    Case 1
        s1 = CStr(Val(s1) + Val(s2))
    Case 2
        s1 = CStr(Val(s1) - Val(s2))
    Case 3
        s1 = CStr(Val(s1) * Val(s2))
    Case 4
        s1 = CStr(Val(s1) / Val(s2))
    End Select
    a.SetFocus
End Sub
'初始化相关数据
Private Sub initValue()
    Text1.Text = "0"
    s1 = ""
    s2 = ""
    isClickNumLast = False
    isClickOkLast = False
    flag = 1
End Sub
'复制
Private Sub mnuEditCopy_Click()
    Clipboard.Clear
    Clipboard.SetText Text1.Text
End Sub
'关于
Private Sub mnuHelpAbout_Click()
    MsgBox "calc | 简易计算器" & vbCrLf & vbCrLf & _
        "  作者:sysdzw" & vbCrLf & _
        "  主页:http://hi.baidu.com/sysdzw" & vbCrLf & _
        "  Q  Q:171977759" & vbCrLf & _
        "  邮箱:sysdzw@163.com" & vbCrLf & vbCrLf & _
        "2007-10-24", vbInformation
End Sub

Private Sub mnuViewScience_Click()
    mnuViewStand.Checked = False
    mnuViewScience.Checked = True
End Sub

Private Sub mnuViewSep_Click()
    mnuViewSep.Checked = Not mnuViewSep.Checked
End Sub

Private Sub mnuViewStand_Click()
    mnuViewStand.Checked = True
    mnuViewScience.Checked = False
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -