Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Last time I asked a question whether a textbox and buttons can be movable inside user control and Abhinav S. posted a useful link.

Now that my textbox and buttons are movable, I would like to know if the line I created using paint event can be movable too.

Help me guys, Thanks! :}
Posted

Only sort of.
When you draw a line in the Paint event, it doesn't really "exist" in the same way that a control does: it will disappear the next time the control it was drawn in is invalidated, unless you re-draw it. If you draw it in a different position each time, then it will appear to move.

Try it: Set up a timer, which moves an integer up by 10 each time, wrapping at 100 and use that as the Y coordinate at which you draw your line. Call the controls Invalidate method, and you will see the line move.
 
Share this answer
 
Comments
yummy02 3-Feb-11 21:53pm    
+5 :) Now I realized it's so true..
Since you have already drawn the line, I assume you have handled MouseDown, MouseMove and MouseUp events for the container.

Now, when the MouseDown happens, check if the click is one a existing line (you will have to keep a reference to the line coordinates as an when you create them). If yes, get that very point on the line and do not repaint it. Get the coordinates in the MouseMove and figure out the changes that should happen to the original start and the end points of the line. This should give you the new start and end points. Use them to redraw.

Suppose you have clicked on the line at (x,y). In MouseMove check the cooridnated again, say (x + 3,y-4). Now if the original start and end points were (origStartX,origStartY) and (origEndX, origEndY), they too will change to (origStartX + 3,origStartY - 4) and (origEndX + 3, origEndY - 4). Draw the line using these points.

In MouseUp, you will do the same recalculation for the last time. Once it is done, update the references and change the values for the original points to the new point.
 
Share this answer
 
Comments
yummy02 17-Jan-11 21:30pm    
What if I would like to have a movable textbox that when I will move it, the label over textbox will follow... Is that possible?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900