-
vb.net教程之按钮(二)
按钮(二)
下面会复杂一些,我们先跳过等号,而来处理四则运算:
先在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 = StrNum
StrNum1 = ""’字符串1清空
End If
Runsign = Index - 11 ‘储存键入的运算符
里面运算的部分我用了一个过程,是因为在按下等号时还会调用到运算过程,这样可以减少代码的输入,也使程序便于浏览。建立过程(Procedure),可以在General部分直接输入,也可以通过菜单里的“Add Procedure”添加。Run的代码如下:
Sub Run()
Num1 = Val(StrNum2)
Num2 = Val(StrNum1)
Select Case Runsign
Case
equal = Num1 + Num2
Case
equal = Num1 - Num2
Case
equal = Num1 * Num2
Case
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
{C}{C}
这样完整的程序就完成了,本章介绍了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 周晓阳。
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。如图4-8。填写代码后运行:
{C}{C}
Private Sub cmdCancel_Click()
MsgBox "Cancel按钮被按下", vbDefaultButton1, "提示"
End Sub
Private Sub cmdOK_Click()
MsgBox "OK按钮被按下", vbDefaultButton1, "提示"
End Sub
结果如图4-9、4-10:
{C}{C}{C}{C}{C}
快捷键大家一定都知道,在VB如何实现呢。其实很简单,在刚才的例子里在建立一个按钮Command1,把它的Caption属性设为Click &ME,如图4-11,在M的下面有一条下划线,运行程序,当你按下Alt+M的时候就触发了Command1_Click事件:
{C}{C}
Private Sub Command1_Click()
MsgBox "Click ME 按钮被按下", vbDefaultButton1, "提示"
End Sub
据我所知,有许多用户有使用快捷键的习惯,注意细节上的问题能够使将来你的程序得到更多人的欣赏,不是吗?