如何在VB.NET中处理KeyDown

Ton*_*y C 2 vb.net

好吧,这是我的困境.这是我的代码:

   If e.KeyCode = Keys.A Then
        TextBox1.AppendText("C, ")
        PictureBox2.Visible = True
        My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
   End If
Run Code Online (Sandbox Code Playgroud)

现在当我在Form1_KeyDown下输入它时,visual basic认为:

'KeyCode不是'System.EventArgs'的成员

现在我已经看过这个代码了,但它不在这里.有帮助吗?

这是完整的代码:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    If e.KeyCode = Keys.A Then
        TextBox1.AppendText("A, ")
        PictureBox2.Visible = True
        My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    End If
    If e.KeyCode = Keys.S Then
        TextBox1.AppendText("C,")
        PictureBox14.Visible = True
        My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

Nol*_*rin 6

不确定为什么你的方法定义声明eEventArgs,但修复只是使参数类型KeyEventArgs.这是因为EventArgs(自然地)不包含被称为的属性KeyCode,但KeyEventArgs确实如此!

将事件处理程序方法定义更改为以下内容:

Private Sub foo_KeyDown(sender As Object, e As KeyEventArgs)
    If e.KeyCode = Keys.A Then
        TextBox1.AppendText("A, ")
        PictureBox2.Visible = True
        My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    ElseIf e.KeyCode = Keys.S Then
        TextBox1.AppendText("C,")
        PictureBox14.Visible = True
        My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)