Dragging controls at runtime

TexasAggie
10-27-2006, 11:53 AM
I'd like to be able to move controls around at runtime. I tried this:


Private Sub txtTime_Mousemove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtTime.MouseMove
txtTime.Location = New Point(e.X, e.Y)
End Sub


but it doesn't work smoothly. What is the best way?

Thanks.

wayneph
10-27-2006, 12:36 PM
Generally you need to use the MouseDown, MouseMove and MouseUp events together.

In MouseDown, set a varialbe that says you're dragging.
In MouseMove check the variable to see if you're dragging.
If Dragging, then calculate the new position of teh control.
In Mouse Up clear the variable.

reboot
10-27-2006, 12:39 PM
Also in that event, e.X and e.Y are coordinates of your textbox, not the form.
I bet it bet it jerks around frantically, right?

TexasAggie
10-27-2006, 12:40 PM
Yes, it does.

TexasAggie
10-27-2006, 12:50 PM
How do I get the cursors location with respect to the form?

wayneph
10-27-2006, 12:58 PM
I do mostly WebApps, but it sounds like Form.MousePosition (http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.mouseposition.aspx) is what you're looking for.

talyrond
10-29-2006, 02:23 PM
Hi, check this link, I did a sample project that I think will help:

http://www.xtremedotnettalk.com/showthread.php?t=91668

jwakeman
10-30-2006, 11:07 AM
Here is another thread that might be helpful:

http://www.xtremevbtalk.com/showthread.php?t=262333

You can see in post #10 that I posted some code which allows the user to use a marquee to select a number of controls and drag/drop and well as cut and paste the controls.

I can post the actual project to this thread if you like.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum