tags:

views:

1223

answers:

4

Ok here's my dilemma. here's this code I have:

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

Now when I enter this under Form1_KeyDown, visual basic thinks this:

'KeyCode is not a a member of 'System.EventArgs'

Now I've seen this code work before, but it isn't here. Any help?

Here's the full code:

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
+4  A: 

Not sure why you method definition declares e as EventArgs, but the fix is simply to make the parameter of type KeyEventArgs. This is because EventArgs (naturally) does not contain a property called KeyCode, but KeyEventArgs does!

Change your event handler method definition to the following:

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
Noldorin
And, not least, it is part of the signature of the KeyDown event
Fredrik Mörk
@Fredrik: Yeah, that's the more conceptual way to look at it.
Noldorin
I suggest you give your controls better names than TextBox1 and PictureBox2 btw.
Noldorin
@Noldorin, This is just a teazer code, The actual code is much more indepth, I just thought this would be easier for right now.
Tony C
@Tony C: That's fair enough. Is your question answered now, though?
Noldorin
@Noldorin, Kinda. I know how to do it know, however, it still doesn't work. Thanks for the advice though, it will come in handy.
Tony C
A: 

I think you're suppose to use:

e As System.Windows.Forms.KeyEventArgs
CD
A: 

It sounds like your method is using the wrong EventArgs. The Control.KeyDown event sends it as a System.Windows.Forms.KeyEventArgs. So your code should read as

Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
   // code here
End Sub
Joshua
Well the system doesn't register it as an error anymore, but now it won't play the sound.
Tony C
Unfortuantely VB.Net isn't my strong suit but I would try changing the location parameter from My.Resources.C to the actual path and see if that plays it. I'm assuming My.Resources.C is the location to the file or is it a byte array?
Joshua
A: 

I've found sometimes you need to provide the full object type similar to the below:

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

End Sub

Have you also tried testing this in a textbox on the form similar to the below (textbox in example is named 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
Ben Bahrenburg
@Quest4Denali, no I haven't. I'll try it though.
Tony C