求个VB实现简单计数器的代码
发布网友
发布时间:2022-04-06 12:48
我来回答
共3个回答
热心网友
时间:2022-04-06 14:17
新建一个VB工程,双击窗体把复制下面代码即可。
Option Explicit
Private sCount As Long
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 49 '加1
If Shift = 2 Then
Form1.Caption = "CTRL + 1"
sCount = sCount + 1
End If
Case 50 '减1
If Shift = 2 Then
Form1.Caption = "CTRL + 2"
sCount = sCount - 1
End If
Case 51 '清空
If Shift = 2 Then
Form1.Caption = "CTRL + 3"
sCount = 0
End If
Case Else
End Select
Label1.Caption = sCount
End Sub
Private Sub Form_Load()
sCount = 0
Label1.Caption = sCount
End Sub
如果有不明白的地方可以与我交流
jialiu830205@163.com
热心网友
时间:2022-04-06 15:35
对楼上的补充完善一下、增加对小键盘的支持
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim m As String
m = Label1.Caption
If (KeyCode = 49 Or KeyCode = 97) And Shift = 2 Then Label1.Caption = Str(Val(m) + 1)
If (KeyCode = 50 Or KeyCode = 98) And Shift = 2 Then Label1.Caption = Str(Val(m) - 1)
If (KeyCode = 51 Or KeyCode = 99) And Shift = 2 Then Label1.Caption = 0
End Sub
热心网友
时间:2022-04-06 17:10
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim m As String
m = Label1.Caption
If KeyCode = 49 And Shift = 2 Then Label1.Caption = Str(Val(m) + 1)
If KeyCode = 50 And Shift = 2 Then Label1.Caption = Str(Val(m) - 1)
If KeyCode = 51 And Shift = 2 Then Label1.Caption = 0
End Sub