|
-
Mar 21st, 2010, 08:29 AM
#1
Thread Starter
New Member
User Picturebox Moving In Runtime..
Hi all, hope you're well. New user here! Help me please!
I'm creating an application in visual basic, basically a part of it is where I hope to achieve the function whereby users can move pictureboxs(images) when visual basic is running.
Basically, i've got several picture boxes that obviously cannot be moved around the form at the minute.
For example, i want to be able the user to be able to move picturebox1 around the form when they click on it and move it where they want to as opposed to what it does now - doesnt move.
I'm sure this is easy for many, but im a novice at VB so any help would be great in terms of code or knowledge etc.
Thanks
-
Mar 21st, 2010, 08:54 AM
#2
Junior Member
Re: User Picturebox Moving In Runtime..
I'm not sure this is the best answer, but it should get you started...
Create a timer with a quick interval like 100.
Once the mouse clicks down on the picture, enable the timer.
Once the mouse is released, disable the timer.
Put your code to move the picture box inside the timer Tick event.
So basically, the timer executes the code while the mouse is down.
You'll need to grab the mouse cursor position, then position the picture box relative to that. It should look something like this...
Code:
Private Sub TimerMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMove.Tick
Dim pos As Point
pos = Cursor.Position
PictureBox.Top = pos.Y
PictureBox.Left = pos.X
End Sub
-
Mar 21st, 2010, 09:00 AM
#3
Thread Starter
New Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by virtueone
I'm not sure this is the best answer, but it should get you started...
Create a timer with a quick interval like 100.
Once the mouse clicks down on the picture, enable the timer.
Once the mouse is released, disable the timer.
Put your code to move the picture box inside the timer Tick event.
So basically, the timer executes the code while the mouse is down.
You'll need to grab the mouse cursor position, then position the picture box relative to that. It should look something like this...
Code:
Private Sub TimerMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerMove.Tick
Dim pos As Point
pos = Cursor.Position
PictureBox.Top = pos.Y
PictureBox.Left = pos.X
End Sub
Thanks Virtue, that's much appreciated. I will try that tonight or tomorrow and get back to you with the result!
-
Mar 21st, 2010, 10:38 AM
#4
Re: User Picturebox Moving In Runtime..
try the move / resize runtime controls link in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 12:45 PM
#5
Junior Member
Re: User Picturebox Moving In Runtime..
Paul, nice work, those runtime controls are great! They are a bit complicated to understand as a novice.
AustinPowers, here's a simpler version for you:
Code:
Dim Moveable As Boolean
Dim LastPos As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Moveable = True
LastPos = Cursor.Position - PictureBox1.Location
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Moveable Then
PictureBox1.Location = Cursor.Position - LastPos
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Moveable = False
End Sub
Modified from this article: http://www.vbforums.com/showthread.php?t=492261
(thanks to .paul. and Mal1t1a)
Last edited by virtueone; Mar 21st, 2010 at 12:49 PM.
-
Mar 21st, 2010, 04:11 PM
#6
Thread Starter
New Member
Re: User Picturebox Moving In Runtime..
Thanks Paul and Virtue.
Virtue, if i post that code behind my picturebox tomorrow should it run in runtime?
Thanks (im a complete novice lol)
-
Mar 21st, 2010, 04:18 PM
#7
Re: User Picturebox Moving In Runtime..
that won't work. you can't subtract point from point that way
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 04:25 PM
#8
Thread Starter
New Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by .paul.
that won't work. you can't subtract point from point that way
Oh . Can you tell me how to fix it please?
What are you Paul, a programmer by trade?
-
Mar 21st, 2010, 04:32 PM
#9
Re: User Picturebox Moving In Runtime..
 Originally Posted by AustinPowers
Oh  . Can you tell me how to fix it please?
this works:
vb Code:
Dim Moveable As Boolean
Dim LastPos As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Moveable = True
Dim cp As Point = Cursor.Position
cp.Offset(-PictureBox1.Location.X, -PictureBox1.Location.Y)
LastPos = cp
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Moveable Then
Dim cp As Point = Cursor.Position
cp.Offset(-LastPos.X, -LastPos.Y)
PictureBox1.Location = cp
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Moveable = False
End Sub
 Originally Posted by AustinPowers
What are you Paul, a programmer by trade?
not officially
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 04:33 PM
#10
Thread Starter
New Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by .paul.
this works:
vb Code:
Dim Moveable As Boolean
Dim LastPos As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Moveable = True
Dim cp As Point = Cursor.Position
cp.Offset(-PictureBox1.Location.X, -PictureBox1.Location.Y)
LastPos = cp
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Moveable Then
Dim cp As Point = Cursor.Position
cp.Offset(-LastPos.X, -LastPos.Y)
PictureBox1.Location = cp
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Moveable = False
End Sub
not officially 
thanks paul, legend, many thanks to you and virtue!
-
Mar 21st, 2010, 06:33 PM
#11
Junior Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by .paul.
that won't work. you can't subtract point from point that way
Paul, I tested the example before I posted and it worked fine. ???
-
Mar 21st, 2010, 06:38 PM
#12
Re: User Picturebox Moving In Runtime..
try it with option strict on, which is highly recommended by most members on this site
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 06:51 PM
#13
Junior Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by .paul.
try it with option strict on, which is highly recommended by most members on this site
Paul, would you mind elaborating on this? I am a novice as well, and I don't know much about that option or its effects.
-
Mar 21st, 2010, 06:55 PM
#14
Re: User Picturebox Moving In Runtime..
yeah sure. it's:
Project-->Properties-->Compile-->Option Strict
it helps you write your code so you don't leave things which could cause a runtime error at some time.
here's a better explanation
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 21st, 2010, 06:59 PM
#15
Junior Member
Re: User Picturebox Moving In Runtime..
 Originally Posted by .paul.
yeah sure. it's:
Project-->Properties-->Compile-->Option Strict
it helps you write your code so you don't leave things which could cause a runtime error at some time.
here's a better explanation
Very helpful! Thank you sir!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|