In Visual Basic, I try to move 2 Picture Boxes in my pong game using my keyboard ( 'W' and 'S' keys and up and down arrows).
They won't move if there are any buttons or text boxes on the screen, however they will when I remove the the button/text box. I think it might have to do with being able to use the arrow keys to navigate between them but I don't know how exactly to stop that from happening.
Using these events to move up, down,
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
moveUp()
e.Handled = True
Case Keys.Down
moveDown()
e.Handled = True
End Select
End Sub
Private Sub Form1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
Select Case e.KeyCode
Case Keys.Up
moveUp()
Case Keys.Down
moveDown()
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
Select Case e.KeyCode
Case Keys.Up, Keys.Down
stopMoving()
e.Handled = True
End Select
End Sub
Private Sub moveUp()
Me.Text = "Moving Up"
End Sub
Private Sub moveDown()
Me.Text = "Moving Down"
End Sub
Private Sub stopMoving()
Me.Text = "Idle"
End Sub
you can also use the same events to handle up, down on the TextBoxes and Buttons. Add the handlers programmatically
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
AddHandler c.KeyDown, AddressOf Form1_KeyDown
AddHandler c.PreviewKeyDown, AddressOf Form1_PreviewKeyDown
AddHandler c.KeyUp, AddressOf Form1_KeyUp
Next
End Sub
Note: it will only add the handlers to controls directly on your form, not controls inside containers. Though, you can use an extension method to get all children, no matter how buried in containers. See https://stackoverflow.com/a/21388034/832052