Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm 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 don't you know.

 

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

 

I've 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:)

My website
Posted

Coding something like that should be easy, just put a picturebox/imagebox on the form, set it's Picture property then place a timer on the form and set an Interval.

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted
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.
My website
Posted

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 it's 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?

My website
Posted

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

vbnetdraganddropdemo.zip

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted
This tutorial, however is the same as the others, drag contents rather than the whole control
My website
Posted
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.
C#
Guest mutant
Posted

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:

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.

Posted
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 :-)
My website
Guest mutant
Posted

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 :)

Guest mutant
Posted

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 control's 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.

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:

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:

windowsapplication3.zip

  • 2 weeks later...
Guest Deleted member 22320
Posted
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.
Guest Deleted member 22320
Posted

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 you'll see it jerking around and wont be smooth.

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...