-
My cousin is working on a VB program for school and is wondering how to make it so that when he has "compiled" (I don't know a lot about VB, so I don't know how you go about doing things) the program, he can use the arrow keys to move an object. He really needs to know how to do this fast, because it was due a couple of days ago. Please respond ASAP.
[email protected]
-
Hi,
Try:
In a Keydown event:
if keycode=VbKeyDown then
Object.Left= Object.Left + 100
Object.Top = Object.top + 100
end if
You got the idea ??
Good Luck.
-
This moves a picturebox around the screen but the same methods can be used to move other controls as well. First set the form's KeyPreview property to True, then add this code:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Const NUDGE = 100
Select Case KeyCode
Case vbKeyRight
If Picture1.Left + Picture1.Width + NUDGE >= Form1.ScaleWidth Then
' The picture has reached or almost reached the right-hand
' border of the form. Move it only as far as it can go without
' it crossing the border.
Picture1.Left = Form1.ScaleWidth - Picture1.Width
Beep
Else
Picture1.Left = Picture1.Left + NUDGE
End If
Case vbKeyLeft
If Picture1.Left - NUDGE <= 0 Then
Picture1.Left = 0
Beep
Else
Picture1.Left = Picture1.Left - NUDGE
End If
Case vbKeyUp
If Picture1.Top - NUDGE <= 0 Then
Beep
Picture1.Top = 0
Else
Picture1.Top = Picture1.Top - NUDGE
End If
Case vbKeyDown
If Picture1.Top + Picture1.Height + NUDGE >= Form1.ScaleHeight Then
Beep
Picture1.Top = Form1.ScaleHeight - Picture1.Height
Else
Picture1.Top = Picture1.Top + NUDGE
End If
End Select
End Sub