在VB中,怎样把数字键1-9与command1(0-8)进行连接,

来源:百度知道 编辑:UC知道 时间:2024/09/22 16:34:59
比如单击1 ,其中1的keyacsii=49 那么我就要单击数字1就可以单击command1(0) 谢谢大家解决!

'假设窗体上有10个按钮,分别为command1(0-9),还有一个Text1,点击按钮时在Text1中输入对应的字符
'如果需要按下小键盘的0-9时分别单击对应的按钮,则使用:

Private Sub Command1_Click(Index As Integer)
Text1.SelText = Index
End Sub

Private Sub Command1_KeyUp(Index As Integer, KeyCode As Integer, Shift As Integer)
'数字键盘的0-9对应的keyCode为96-105
If KeyCode > 95 And KeyCode < 106 Then
Command1(KeyCode - 96).SetFocus
Command1_Click (KeyCode - 96)
End If
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode > 95 And KeyCode < 106 Then
Command1(KeyCode - 96).SetFocus
Command1_Click (KeyCode - 96)
End If
End Sub

Private Sub Form_Load()
For i = 0 To 9
Command1(i).Caption = i
Next
Text1 = ""
Text1.Locked = True '禁止用户直接键入,只能通过单击按键才能输入,防止输入非数字字符
End Sub

把工程中的窗体 keypreview 设为