• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Drag and Drop in VB

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

perfectturmoil

Member
Joined
Oct 23, 2004
Location
Hillbillyville
Hey.

I want to have a thing where you can drag and drop a picturebox with the mouse, and i want the picture to move with the mouse (so you can see where you are dropping the picture).

This seems like it should be an easy thing, but VB is fighting me the whole way ;- ]

I can't do it based on mousedowns from the form, because if you mousedown on the image, it doesn't count as being on the form. My original idea was for the form to pick up mousedowns and mousemoves, and while it was doing that, change the location of the picturebox. BUT, VB won't let you mousedown THROUGH a picturebox.

Doing it on the picturebox has given me a lot of problems with the referencing.. Because then the mouse coordinates are based inside the picture itself (is there a way to get the absolute coordinates?) Then moving the image gets all screwy.. Because moving the mouse moves the picture which changes the coordinates in a different way so hte picture moves elsewhere... Boo..

I was trying to use a timer to control it, but I don't know how to get the mouse coordinates into the timer..

So.. Anyone have any advice on how to make it so you can drag around a picturebox and have it show you the picturebox moving?

Thanks!

(Using VB.net 2005 :- ] )
 
Try this:

Code:
Public Class Form1
    Private dragging As Boolean = False
    Private ptDragOffset As Point

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        'Offset of mouse (Left, Top) where drag is started (Picture Box Client Coordinates)
        ptDragOffset = New Point(e.X, e.Y)
        dragging = True
    End Sub

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

    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If (Not dragging) Then Return

        ' Convert Mouse Position to Screen Coordinates
        Dim ptScreen As Point = PictureBox1.PointToScreen(New Point(e.X, e.Y))

        ' Now Convert to Picture Box Parent Coordinages
        Dim ptForm As Point = PictureBox1.Parent.PointToClient(ptScreen)

        ' Set the position of the picture box relative to its parent
        PictureBox1.Left = ptForm.X - ptDragOffset.X
        PictureBox1.Top = ptForm.Y - ptDragOffset.Y
    End Sub
End Class
 
Thats perfect. And it makes so much sense :- ]

I haven't yet become familiar with a lot of the lesser known ways of doing things in VB.. I am fairly comfortable with the basics.. But sometimes VB doesn't allow the basics to do things, and I have no real way of exposing myself to the 'right' way of doing things :- ]

The pointtoscreen stuff - at a quick look those things grab the points and converts them to refer to the different references, or something like that.. I couldn't figure out how to do that..

Thanks.. I owe you a dollar :- ]
 
A dollar, coooool. Do you want my paypal email address? Glad I could help.

That is the one problem with the DotNet framework, especially for new developers. The framework so HUGE, knowing where to start or even what to search for can be a problem. I started many years ago before .NET, Visual Basic, MFC (Microsoft Foundation Classes), etc. Win API all the way... you had to hand code everything from message loops on up. Luckily, that gives you mega insight into what needs to be done at the more abstract levels (like .NET). You are doing the same thing, .NET just wraps everything up in pretty classes.
 
marker said:
A dollar, coooool. Do you want my paypal email address? Glad I could help.

That is the one problem with the DotNet framework, especially for new developers. The framework so HUGE, knowing where to start or even what to search for can be a problem. I started many years ago before .NET, Visual Basic, MFC (Microsoft Foundation Classes), etc. Win API all the way... you had to hand code everything from message loops on up. Luckily, that gives you mega insight into what needs to be done at the more abstract levels (like .NET). You are doing the same thing, .NET just wraps everything up in pretty classes.

Yeah.. Thats one thing I've seen.. I really feel like a lot of things would have been SOO much easier in VB6.. Just because people have done it before.. All the examples for EVERYTHING I've wanted to do was in VB6.. and it all seems easy..

.NET brings a lot of advantages with interactions and stuff.. But it seems like they've redone so many things that I myself want to do :- ]

And if you give me your paypal address, I'll give you a dollar (or your real addy, I'll send you a nice crisp george washington :- ] )
 
Back