2009-07-24 47 views
2

好的,這是我的困境。這裏的這段代碼我:當我進入這個Form1_KeyDown下如何處理VB.NET中的KeyDown

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

現在,Visual Basic中認爲這:

「邀請碼不是AA成員 '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 
+0

您可以包括完整的事件處理方法(包括小組聲明)的代碼? – 2009-07-24 13:56:14

回答

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 
+1

而且,至少它是KeyDown事件的簽名的一部分 – 2009-07-24 13:57:49

+0

@Fredrik:是的,這是更概念的方式來看待它。 – Noldorin 2009-07-24 14:00:38

+0

我建議你給你的控件比TextBox1和PictureBox2更好的名稱。 – Noldorin 2009-07-24 14:05:09

0

這聽起來像你的方法是使用了錯誤的EventArgs。 Control.KeyDown事件將其作爲System.Windows.Forms.KeyEventArgs發送。所以,你的代碼應爲

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

我發現有時你需要提供類似下面的完整對象類型:

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 
0

這應該在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 
相關問題