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

程序代码:

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键盘代码


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


上一篇:springboot配置过滤器导致的controller入参丢失问题


下一篇:如何初始化centos或debian系统(公司)