我想知道是否有人可以帮我解决上述问题。
基本上我所拥有的是一个标签,其中随机生成了一个字母。
我想要做的是当按下标签中的相同键时显示一个msgbox(只是为了表明它现在有效)。我尝试了两种方法,但似乎都没有用,有人能指出我正确的方向吗?它似乎并不那么难,我只是新编码。
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar = lblletter1.Text.ToString) Then
MsgBox("WORKS")
ElseIf (e.KeyChar = lblletter2.Text) Then
MsgBox("words")
End If
非常感谢!
答案 0 :(得分:1)
要处理表单的KeyPress
事件,您需要设置它的KeyPreview
属性。您可以在表单设计器或表单的Load
事件处理程序中执行此操作,如下所示。
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = lblletter1.Text(0) Then
MessageBox.Show("WORKS")
ElseIf e.KeyChar = lblletter2.Text(0) Then
MessageBox.Show("words")
End If
End Sub
End Class
答案 1 :(得分:0)
试试这个
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.ToString = lblletter1.Text) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.ToString = lblletter2.Text) Then
MsgBox("words")
End If
End Sub
答案 2 :(得分:0)
试试这个
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.Trim() = lblletter1.Text.Trim()) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.Trim() = lblletter2.Text.Trim()) Then
MsgBox("words")
End If
End Sub