VB编程:全局变量&控件数组实例简单计算器-12

运行效果:
VB编程:全局变量&控件数组实例简单计算器-12

程序代码:
Dim current, prev, choice    '全局变量
'-------------这段代码是个额外加上的小测试可以忽略----------
'---------------用API来关闭正在运行运行的程序---------------
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_CLOSE = &H10

Private Sub CmdEnd_Click()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, "简单计算器")  '此处为程序的Caption值
    Debug.Print winHwnd
    If winHwnd <> 0 Then
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
        If RetVal = 0 Then
            MsgBox "植入消息错误!"
        End If
    Else
        MsgBox "程序没有打开!"
    End If   
End Sub
'------------------这段代码可以删除不影响程序运行-------------------

Private Sub CmdAc_Click()
Text1.Text = ""      '清空输入框
End Sub

Private Sub CmdEquel_Click()             '执行“=”
    current = Val(Right(Text1.Text, Len(Text1.Text) - Len(prev) - Len(choice))) '获取后半截数字
    If choice = "+" Then
        current = prev + current
        Text1.Text = Text1.Text & "=" & current
    ElseIf choice = "-" Then
        current = prev - current
        Text1.Text = Text1.Text & "=" & current
    ElseIf choice = "*" Then
        current = prev * current
        Text1.Text = Text1.Text & "=" & current
    ElseIf choice = "/" And current <> 0 Then
        current = prev / current
        Text1.Text = Text1.Text & "=" & current
    Else
        Text1.Text = "Press AC to Continue"
    End If    
End Sub

Private Sub CmdPlus_Click()
    prev = Text1.Text
    choice = "+"
    Text1.Text = Text1.Text & "+"
End Sub

Private Sub CmdMinus_Click()
    prev = Text1.Text
    choice = "-"
    Text1.Text = Text1.Text & "-"
End Sub

Private Sub CmdAsterisk_Click()
    prev = Text1.Text
    choice = "*"
    Text1.Text = Text1.Text & "*"
End Sub

Private Sub CmdSlash_Click()
    prev = Text1.Text
    choice = "/"
    Text1.Text = Text1.Text & "/"
End Sub

Private Sub CmdPoint_Click()
    Text1.Text = Text1.Text & "."
End Sub

Private Sub Command1_Click(Index As Integer)
    Text1.Text = Text1.Text & Index             '控件数组的Index属性值来输入0-9
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)   '键盘输入值保存到定义好的变量中
    Select Case KeyCode
        Case 13  '回车
            Call CmdEquel_Click
        Case 107
            prev = Text1.Text
            choice = "+"
        Case 109
            prev = Text1.Text
            choice = "-"
        Case 106
            prev = Text1.Text
            choice = "*"
        Case 111
            prev = Text1.Text
            choice = "/"
        Case 35     'End键
            Call CmdAc_Click
    End Select
End Sub

编写心得:
    1、这个实例体现了控件数组的优越性,不用每一个按钮去添加0-9数字执行的过程。
    2、开头出代码绑定了“End”命令按钮,用API来关闭正在运行的程序,感觉没多大用,就我目前的知识水平,感觉这大段代码的作用等同于“End”,希望在今后的学习中能够了解其更深层次的用法。
    3、此处运用了KeyCode键盘代码,附图如下:
VB编程:全局变量&控件数组实例简单计算器-12

附件:源程序下载链接:http://pan.baidu.com/s/1qYrRKTm 密码:xwqn

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

彭世瑜

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值