繁体   English   中英

如何在VB.NET中处理KeyDown

[英]How to handle KeyDown in 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

现在,当我在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

不知道为什么您的方法定义将e声明为EventArgs ,但解决方法只是使参数成为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

听起来您的方法使用了错误的EventArgs。 Control.KeyDown事件将其作为System.Windows.Forms.KeyEventArgs发送。 所以你的代码应该读为

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
   // code here
End Sub

我发现有时候您需要提供类似于以下内容的完整对象类型:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MsgBox(e.KeyCode.ToString()) 'Message what the keycode
If (e.KeyCode = Keys.A) Then
    MsgBox("e.KeyCode = Keys.A") 'Message that I've found the A
    TextBox1.AppendText("C, ")
    PictureBox2.Visible = True
    My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If

结束子

您是否也尝试过在类似于以下形式的文本框中测试此内容(示例中的文本框名为TextBoxKeyTest)?

    Private Sub TextBoxKeyTest_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxKeyTest.KeyDown
    MsgBox(e.KeyCode.ToString())'Message what the keycode
    If (e.KeyCode = Keys.A) Then
        MsgBox("e.KeyCode = Keys.A")'Message that I've found the A
        TextBox1.AppendText("C, ")
        PictureBox2.Visible = True
        My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
    End If
End Sub

这应该在VB 2010中起作用

Private Sub cmdWiden_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.Up Then Me.Top = Me.Top - 5
        If e.KeyCode = Keys.Down Then Me.Top = Me.Top + 5
        If e.KeyCode = Keys.Left Then Me.Left = Me.Left - 5
        If e.KeyCode = Keys.Right Then Me.Left = Me.Left + 5
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM