PDA

Click to See Complete Forum and Search --> : simple movement of a picturebox....


james14
Jan 29th, 2003, 03:54 PM
I want to have a sample code so that i can look at, but what i want it to be is a code that will hav ethe picturebox1 move to whereever the mouse clicked so i need:

1.)On mousedown move pctrbox1 there
2.)make it so i can understand.

Thank you so much, next is going to be map :rolleyes:

MrPolite
Jan 29th, 2003, 05:21 PM
hmm is this what you want? or do you want to be able to mouse the picturebox as you move the mouse?
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
PictureBox1.Location = New Point(e.X, e.Y)
End Sub
this will move the picturebox to where you clicked on the form.


If you want to be able to drag the picturebox around then use this:
Private isMouseDown As Boolean = False
Private initX, initY As Integer

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
isMouseDown = True
initX = e.X
initY = e.Y
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If isMouseDown Then
PictureBox1.Left += e.X - initX
PictureBox1.Top += e.Y - initY
End If
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
isMouseDown = False
End Sub

james14
Jan 29th, 2003, 05:27 PM
Ok thank you soo much if i have any questions ill ask you

MrPolite
Jan 29th, 2003, 05:33 PM
np:)
just something that might be helpful to mention: note that in the first code, in the form_mousedown event, e.x and e.y are the location of mouse in the form. But in the second part of the code, e.x and e.y are the location of the mouse in the picturebox

james14
Jan 31st, 2003, 09:45 PM
OK mr.polite i have another question: How can i make it so that the box wouldnt move instantly? like it would go to the destination one square at a time? Also how cna i set boundries?

MrPolite
Jan 31st, 2003, 09:56 PM
Originally posted by james14
OK mr.polite i have another question: How can i make it so that the box wouldnt move instantly? like it would go to the destination one square at a time? Also how cna i set boundries? hmm what do you mean? so when someone clicks it would start to move to that location ? like an animation?

james14
Feb 1st, 2003, 09:13 AM
Well,yeah kind of like that but say X is were i am and i clicked to Y

X
-
-
-
-
Y

It would move straight down to the Y. It would slide down to the Y going to all of the - spaces before it got to Y... i hope you understand.

ProgrammerJon
Feb 1st, 2003, 10:34 AM
why don't you use a timer?
(I can't tell you how to use a timer because I don't know how to use it too much)

james14
Feb 1st, 2003, 10:41 AM
no no no you missed the point. hmm you know how in shooting games and stuff? well like that kind of movement. you dont just go there automaticly it moves through the pixels (or square things i think) it doesnt just appear where you clicked it moves up one square at a time

(i dont know hoe to use timers either =))

ProgrammerJon
Feb 1st, 2003, 02:50 PM
Originally posted by james14
no no no you missed the point. hmm you know how in shooting games and stuff? well like that kind of movement. you dont just go there automaticly it moves through the pixels (or square things i think) it doesnt just appear where you clicked it moves up one square at a time

(i dont know hoe to use timers either =)) \


something like this:

for i = 1 to mouseposition
'pause sometime
object.location = object.location + 1
next i