A simple example based upon keypresses on a form. Simple form with one button and picture box on. You are detecting the key that is pressed and moving the picturebox by adjusting the left and top properties.
Public Class Form1
Private Sub MoveButtonAround(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp, Button1.KeyUp If e.KeyCode = Keys.Down Then PictureBox1.Top = PictureBox1.Top + 1 ElseIf e.KeyCode = Keys.Up Then PictureBox1.Top = PictureBox1.Top - 1 ElseIf e.KeyCode = Keys.Left Then PictureBox1.Left = PictureBox1.Left - 1 ElseIf e.KeyCode = Keys.Right Then PictureBox1.Left = PictureBox1.Left + 1 End If End Sub
Yep - I had that code originally and though I'll just set them back to 1 otherwise they might wander why I chose 10 arbitarily.
Now we gave them going we with the demonstration of the top and left property and keyup events. Its a case of refering them to relevent functions for them to check out and try things by themselves.
Cool code Spotty! Here's a minor change that makes it work faster and moves the picturebox as long as the Up, Down, Left, Right arrow keys are held down:
Private
Sub MoveButtonAround(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Down Then
PictureBox1.Top = PictureBox1.Top + 10
ElseIf e.KeyCode = Keys.Up Then
PictureBox1.Top = PictureBox1.Top - 10
ElseIf e.KeyCode = Keys.Left Then
PictureBox1.Left = PictureBox1.Left - 10
ElseIf e.KeyCode = Keys.Right Then
PictureBox1.Left = PictureBox1.Left + 10
End If
End Sub
Of course you know we just did somebody's homework for them!!! :-)
Don't drink too many of those beers!! Keeping the Picturebox that is movable from hitting another Picturebox, requires you to keep track of the location of the moving picturebox. In this case, you need to know what the , Left, Top, Right, Bottom locations of the 2nd box and make sure that no portion of the movable box gets near those locations. Time to do some real coding!! Have fun.
It works in the KeyDown event too. I tested it before posting and again just now. As for keeping the picture box from hitting another picturebox, you are getting into collision detection. More math. Do a search on MSDN or Google for Collision detection, Visual Basic etc and you should find what you need.
Here's how to make the picturebox follow the mouse pointer on a form:
Private
Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Me.PictureBox1.Left = Control.MousePosition.X
Me.PictureBox1.Top = Control.MousePosition.Y
End Sub
That can be adapted to work with the keyboard's Up & Down and Left & Right keys fairly easy.
Could U give me the code from beginning to end on how to make a picture
box move with the arrow keys in visual basic or maybe even the code
from beginning to end on a basic lunar lander game i am new to this i
got it somewhat working but i need some code to look at for reference
Some things you are going to have to learn for yourself, just like the original poster in this thread. The code you need to move a picturebox with the arrow keys is already here. Put a picturebox on a form, load a picture to it, and then in the event cited in the above posts, use that code, and it will move the picturebox around when holding down the arrow keys. That should get you started. Try working on it and when you have specific questions, post them in a NEW THREAD here and we will try to help you. It's best to learn by doing.
I Need ASAP: Keyboard input
Steelhand
:)
==EDIT==
The "move faster" thing dosent work, what i mean is it dosent move when i hold the keys down.
Carlos Figueira - MSFT
but now i need to know how to get one picture box to now be able to go on another.
kind of like collision detection
breedlings
A simple example based upon keypresses on a form. Simple form with one button and picture box on. You are detecting the key that is pressed and moving the picturebox by adjusting the left and top properties.
Public Class Form1
Private Sub MoveButtonAround(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp, Button1.KeyUp
If e.KeyCode = Keys.Down Then
PictureBox1.Top = PictureBox1.Top + 1
ElseIf e.KeyCode = Keys.Up Then
PictureBox1.Top = PictureBox1.Top - 1
ElseIf e.KeyCode = Keys.Left Then
PictureBox1.Left = PictureBox1.Left - 1
ElseIf e.KeyCode = Keys.Right Then
PictureBox1.Left = PictureBox1.Left + 1
End If
End Sub
End Class
Dale57
Yep - I had that code originally and though I'll just set them back to 1 otherwise they might wander why I chose 10 arbitarily.
Now we gave them going we with the demonstration of the top and left property and keyup events. Its a case of refering them to relevent functions for them to check out and try things by themselves.
BringToFront property and SendToBack properties
http://msdn2.microsoft.com/en-us/system.windows.forms.control.bringtofront.aspx
A.J. Mee
Cool code Spotty! Here's a minor change that makes it work faster and moves the picturebox as long as the Up, Down, Left, Right arrow keys are held down:
Private
Sub MoveButtonAround(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Down ThenPictureBox1.Top = PictureBox1.Top + 10
ElseIf e.KeyCode = Keys.Up ThenPictureBox1.Top = PictureBox1.Top - 10
ElseIf e.KeyCode = Keys.Left ThenPictureBox1.Left = PictureBox1.Left - 10
ElseIf e.KeyCode = Keys.Right ThenPictureBox1.Left = PictureBox1.Left + 10
End If End SubOf course you know we just did somebody's homework for them!!! :-)
james
aka:Trucker
espi
Don't drink too many of those beers!! Keeping the Picturebox that is movable from hitting another Picturebox, requires you to keep track of the location of the moving picturebox. In this case, you need to know what the , Left, Top, Right, Bottom locations of the 2nd box and make sure that no portion of the movable box gets near those locations. Time to do some real coding!! Have fun.
james
aka:Trucker
doddy94434
It wont because the keyup event because you have not fired a keyup event.
Jani Pewter
It works in the KeyDown event too. I tested it before posting and again just now. As for keeping the picture box from hitting another picturebox, you are getting into collision detection. More math. Do a search on MSDN or Google for Collision detection, Visual Basic etc and you should find what you need.
james
aka:Trucker
RichV
THANK YOU SO MUCH!!!
Rixandy
Here's how to make the picturebox follow the mouse pointer on a form:
Private
Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove Me.PictureBox1.Left = Control.MousePosition.X Me.PictureBox1.Top = Control.MousePosition.Y End SubThat can be adapted to work with the keyboard's Up & Down and Left & Right keys fairly easy.
james
aka:Trucker
gkb
Peter Butler
Some things you are going to have to learn for yourself, just like the original poster in this thread. The code you need to move a picturebox with the arrow keys is already here. Put a picturebox on a form, load a picture to it, and then in the event cited in the above posts, use that code, and it will move the picturebox around when holding down the arrow keys. That should get you started. Try working on it and when you have specific questions, post them in a NEW THREAD here and we will try to help you. It's best to learn by doing.
james
aka:Trucker
ArgentinianGirl
WilfRosenbaum
starting in 3...
2...
1...
GO!