Moving a picturebox

crystaluz

Well-known member
Joined
Feb 17, 2009
Messages
55
Programming Experience
Beginner
I have a problem :

In VB.NET.

Window Application :

I have a form..in that form..there are four buttons and an image (using picBox). That buttons named as "Up", "Down", "Left", "Right".
How to move the image by using the button. For example :
If I click "Up" button, the image will move upward and so on..
Please..I need your help..Thnx
 
Last edited by a moderator:
PictureBoxes like all other form controls have location, left and top properties that can be changed to move it around. In your case you'll want the left and top properties to be incremented/decremented as the user clicks the various buttons.
 
PictureBoxes like all other form controls have location, left and top properties that can be changed to move it around. In your case you'll want the left and top properties to be incremented/decremented as the user clicks the various buttons.

Sorry...I dont understand..:confused:
 
In the up button's Click event decrement the PB's Top property, for example:
VB.NET:
Private Sub UpButton_Click (...) Handles UpButton.Click
    PictureBox1.Top -= 1 'Change this number if you want it moved more than 1 pixel at a time
End Sub
 
In the up button's Click event decrement the PB's Top property, for example:
VB.NET:
Private Sub UpButton_Click (...) Handles UpButton.Click
    PictureBox1.Top -= 1 'Change this number if you want it moved more than 1 pixel at a time
End Sub


Ok..I got it...
But...that's for move upward..
What about, left, right, and down?

Edit:
Errr....no need i think..I got it already...
Hehe...thank you very much Mode..
Very Helpful..
 
Ok..I got it...
But...that's for move upward..
What about, left, right, and down?
Think about what I said in the other posts in this thread and see how the code for the up button works, how do you think the other buttons would work?
 
Think about what I said in the other posts in this thread and see how the code for the up button works, how do you think the other buttons would work?

Yes...thank you..
The Up button works like this :

PicBox1.Top -= 1
The negative sign makes the picture move upward

To make it move downward...just change it to positive.

Am I right?
 
Yes...thank you..
The Up button works like this :

PicBox1.Top -= 1
The negative sign makes the picture move upward

To make it move downward...just change it to positive.

Am I right?
Yep, how would you move it left and right?
 
Form's have a ClientSize property (width and height) and pictureboxes have a width and a height that can be used to determine when it's on the edge of the form
 
Control.ClientRectangle Property (System.Windows.Forms)

You'll want to put a check in to make sure your move will keep you in the form's ClientRectangle.

Here's an example for moving Left and Right.

VB.NET:
	Private Sub uxLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxLeft.Click

		If (Me.PictureBox1.Left - 10) > 0 Then
			Me.PictureBox1.Left -= 10
		Else
			Me.PictureBox1.Left = 0
		End If

	End Sub

	Private Sub uxRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxRight.Click

		If (Me.PictureBox1.Left + Me.PictureBox1.Width + 10) < Me.ClientRectangle.Right Then
			Me.PictureBox1.Left += 10
		Else
			Me.PictureBox1.Left = Me.ClientRectangle.Right - Me.PictureBox1.Width
		End If

	End Sub
 
Control.ClientRectangle Property (System.Windows.Forms)

You'll want to put a check in to make sure your move will keep you in the form's ClientRectangle.

Here's an example for moving Left and Right.

VB.NET:
	Private Sub uxLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxLeft.Click

		If (Me.PictureBox1.Left - 10) > 0 Then
			Me.PictureBox1.Left -= 10
		Else
			Me.PictureBox1.Left = 0
		End If

	End Sub

	Private Sub uxRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles uxRight.Click

		If (Me.PictureBox1.Left + Me.PictureBox1.Width + 10) < Me.ClientRectangle.Right Then
			Me.PictureBox1.Left += 10
		Else
			Me.PictureBox1.Left = Me.ClientRectangle.Right - Me.PictureBox1.Width
		End If

	End Sub



Below is my code :


Private Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click

If (Me.picBox1.Left - 10) > 0 Then
Me.picBox1.Left -= 10
Else
Me.picBox1.Left = 0
End If


End Sub


Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click

If (Me.picBox1.Left + Me.picBox1.Width + 10) < Me.ClientRectangle.Right Then
Me.picBox1.Left += 10
Else
Me.picBox1.Left = Me.ClientRectangle.Right - Me.picBox1.Width
End If




End Sub

Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click

If (Me.picBox1.Top - 10) > 0 Then
Me.picBox1.Top -= 10
Else
Me.picBox1.Top = 0
End If

End Sub

Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click

If (Me.picBox1.Top + Me.picBox1.Height + 10) > Me.ClientRectangle.Top Then
Me.picBox1.Top += 10
Else
Me.picBox1.Top = Me.ClientRectangle.Top - Me.picBox1.Height

End If



End Sub




All buttons are working properly except "Down" button. When I clicked it, the image still can move outside the form.
How to fix it?
 
The limit when you move down is Bottom, not Top.
 
Back
Top