just for fun

hog

Well-known member
Joined
Mar 17, 2003
Messages
984
Location
UK
Im looking into being able to drag either a button, picturebox, or whatever else can hold a image around a form. As a distraction from database programming dont you know.

Thing is my books talk little of the graphics of VB. NET, well there about databases, so no suprises there:D

Ive been reading the online help about DoDragDrop and MouseDown etc but can get a picturebox or button to move. I keep reading the help but am getting nowhere

Could someone supply me with a code snippet which will get me on my way please:)
 
Coding something like that should be easy, just put a picturebox/imagebox on the form, set its Picture property then place a timer on the form and set an Interval.
Code:
Private Sub Timer1_Tick()
 Picture1.Top += 10
 Picture1.Left += 10
 If Picture1.Top >= Me.Height Then Picture1.Top = 0
 If Picture1.Left >= Me.Width Then Picture1.Left = 0
End Sub
 
No you misunderstand, I know how to do what you have said. What I want to do is allow the user to drag the control around the form.
 
Mmm am I misunderstanding the drag and drop terms?

I have found bits and bobs on the web that demo drag and drop of text, tree nodes and images, but nothing that mentions dragging the actual control itself and not just its contents.

Is it possible to allow a user to drag say a button from one side of a form to another, in a smooth motion?
 
Not that I know of. It wouldnt be too difficult to write code to do it manually though.
 
I find the drag seriously more difficult then it was in VB6, Microsoft seems to have over complicated the procedure. I found this tutorial in some MSDN download I got somewhere

To drag a control, there are some tutorials fo doing this with VB6 on the other forum; You use the MouseDown Event to start a move, MouseMove to follow the mouse, MouseUp to stop the drag
 

Attachments

This tutorial, however is the same as the others, drag contents rather than the whole control
 
This interests me as well. My guess is to use a mousedown event as well as a Mouse move event moving the control according to e.x and e.y as long as mouseup is not reached.
 
To drag a control from one position and drop it in another :D

1. Create a boolean that will hold the flag if the control should be moving.
2. In MouseDown event set the flag to true.
3. In MouseMove check if flag is true. If the flag is true then make the control move with this code:
Code:
Button1.Location = Me.PointToClient(Me.Cursor.Position)
4. In MouseUp event set the moving flag to false.

Thats all folks :)

This will move your control around the form in a nice smooth motion.
 
Thanks Mutant. In the end I went for a point and click approach and skipped the smooth drag motion. Although saying that I did manage to get the smooth drag to work but the control would jump to the cursor position which looked naff. IE user clicks middle of control and starts to drag, the control jumps to the cursor position then starts to drag. I suppose your PointToClient would solve that :-)
 
Well it had that little movement of button on beginning but now I fixed it :)
That gave me an idea :)
And now it has *perfectly* smooth motion :)
 
So bung us the code then......:-) Im sure Ill find a use for it :-))
 
Ill proceed to writing my little guide again :)

1. Dim 4 variables. Moving flag, x as integer, y as integer, and pt as point.
2. In controls MouseDown event set moving flag to true.
3. In MouseUp event set the flag to false.
4. In MouseDown event add following code which will store the location to subtract while the button is moving so cursor doesnt automatically go to upper left corner of control.
Code:
x = CheckBox1.PointToClient(Me.Cursor.Position).X
y = CheckBox1.PointToClient(Me.Cursor.Position).Y
5. In MuseMove check if flag is true. If true then make the button move:
Code:
If moving Then
      Dim pt As Point
      pt.X = Me.PointToClient(Me.Cursor.Position).X - x
      pt.Y = Me.PointToClient(Me.Cursor.Position).Y - y
      CheckBox1.Location = pt
End If
This subracts the position at which the cursor was while in MouseDown. So the control will stay excatly in the same place without moving to cursor. If you drag it by middle, it will be dragged by middle :)

Thats all :)

I noticed a lot of people need this so maybe someone could put this in tutorial forum if needed.

Sample:
 

Attachments

Yea thats how you do it.. I myself made a bunch of user controls that inherit this ability.. if the .FLOAT property is set to true you can drag them around the form using the middle mouse button. I made that for my skin designer program where the need to drag objects around was a big one.
 
one thing to note though. when you are setting the new position of the object use the .location property so you can set both X and Y coordinate simultaniously... otherwise on slower systems and repaints youll see it jerking around and wont be smooth.

like obj.location = new point(x,y)
 
Back
Top