Results 1 to 15 of 15

Thread: User Picturebox Moving In Runtime..

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    8

    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

  2. #2
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    8

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by virtueone View Post
    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!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: User Picturebox Moving In Runtime..

    try the move / resize runtime controls link in my signature

  5. #5
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    8

    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)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: User Picturebox Moving In Runtime..

    that won't work. you can't subtract point from point that way

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    8

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by .paul. View Post
    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?

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by AustinPowers View Post
    Oh . Can you tell me how to fix it please?
    this works:

    vb Code:
    1. Dim Moveable As Boolean
    2. Dim LastPos As Point
    3. Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    4.     Moveable = True
    5.     Dim cp As Point = Cursor.Position
    6.     cp.Offset(-PictureBox1.Location.X, -PictureBox1.Location.Y)
    7.     LastPos = cp
    8. End Sub
    9.  
    10. Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    11.     If Moveable Then
    12.         Dim cp As Point = Cursor.Position
    13.         cp.Offset(-LastPos.X, -LastPos.Y)
    14.         PictureBox1.Location = cp
    15.     End If
    16. End Sub
    17.  
    18. Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    19.     Moveable = False
    20. End Sub


    Quote Originally Posted by AustinPowers View Post
    What are you Paul, a programmer by trade?
    not officially

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    8

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by .paul. View Post
    this works:

    vb Code:
    1. Dim Moveable As Boolean
    2. Dim LastPos As Point
    3. Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    4.     Moveable = True
    5.     Dim cp As Point = Cursor.Position
    6.     cp.Offset(-PictureBox1.Location.X, -PictureBox1.Location.Y)
    7.     LastPos = cp
    8. End Sub
    9.  
    10. Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    11.     If Moveable Then
    12.         Dim cp As Point = Cursor.Position
    13.         cp.Offset(-LastPos.X, -LastPos.Y)
    14.         PictureBox1.Location = cp
    15.     End If
    16. End Sub
    17.  
    18. Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    19.     Moveable = False
    20. End Sub




    not officially
    thanks paul, legend, many thanks to you and virtue!

  11. #11
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by .paul. View Post
    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. ???

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: User Picturebox Moving In Runtime..

    try it with option strict on, which is highly recommended by most members on this site

  13. #13
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by .paul. View Post
    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.

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  15. #15
    Junior Member virtueone's Avatar
    Join Date
    Mar 2010
    Location
    TN, USA
    Posts
    31

    Re: User Picturebox Moving In Runtime..

    Quote Originally Posted by .paul. View Post
    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
  •  



Click Here to Expand Forum to Full Width