vb 关于keydown问题

来源:百度知道 编辑:UC知道 时间:2024/06/16 02:48:52
想根据A,B,C,D,E...到Z的顺序按键后对应的字母标签的字体颜色变红,如果不按照A,B,C,D,E...到Z的顺序按键则不显示红色,,字体还是黑色的

'需要在窗体上创建一个Label1,然后通过复制,粘贴,完成一个Label1(0)-Label1(25)的控件数组,按键盘位置排列好哦

Private Sub Command1_Click() '清除当前所有键的字体颜色
Dim i%
For i = 0 To 25
Label1(i).ForeColor = vbBlack
Next
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode < 65 Or KeyCode > 90 Then Exit Sub
If KeyCode <> 65 Then
If Label1(KeyCode - 66).ForeColor <> vbRed Then Exit Sub
End If
If KeyCode >= 65 And KeyCode <= 90 Then Label1(KeyCode - 65).ForeColor = vbRed
Call ColorChange(KeyCode - 65) '此句实现:按键后只保留当前的键为红色,如果不需要这样,此句可以注释掉
End Sub

Private Sub Form_Load()
Dim i%
Me.KeyPreview = True
Command1.Caption = "全部复原"
If Label1.UBound <> 25 Then MsgBox "请确认Label1控件数组是不是从0到25!": End
For i = 0 To 25
Label1(i).ForeColor = vbBlack
Label1(i).Caption = Chr(65 + i) '将各标签的显示内容变成A-Z
Next
End Sub

Privat