0

The questions says it all.

How can I move a control, say a PictureBox between multiple panels, or betwween a panel and a flow layout panel.

I'm aware I can drag and drop controls between multiple panels and such, however this is does not make the control visually movable between the containers. The mouse only changes to a diferent cursor and after you drag to the other control and release the mouse button the control appears on the other container. I require the control to be visually movable.

Can someone provide a simple example, so I can extract the idea to apply to my situation.

NOTE: Runtime of course.

Fábio Antunes
  • 16,984
  • 18
  • 75
  • 96

2 Answers2

4

Assuming you need it runtime:

  • You can save the control as bitmap using Control.SaveToBitmap method
  • Create cursor from image.
  • Set the current cursor which we created from control.
  • Once drag and drop completed reset the cursor.
Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 1
    I didn't downvote but you seem to miss the point. If I understand correctly, OP wants to be able to move controls during runtime in the same manner as they can be moved in the designer. – BartoszKP Jul 30 '14 at 23:33
  • @SriramSakthivel This is an interesting approach. When the control begins to move the cursor would take the "image" of the control and the control itself would become invisible. Then after dropping the control I would switch the control from one container to another and reset the cursor. Quite interesting. I will try it tomorrow. Thanks. – Fábio Antunes Jul 31 '14 at 02:44
  • @BartoszKP I'm missing what my answer lacks? This works almost same way as designer does. If you want you can hide the control while drag start and make it visible once drag/drop finished – Sriram Sakthivel Jul 31 '14 at 06:03
  • @SriramSakthivel Hi, I've tried your idea. You're not missing anything, this is exactly what I require. My thanks. – Fábio Antunes Jul 31 '14 at 11:07
  • @FábioAntunes Welcome, I'm glad that it helped you. Some dumb people downvote without any valid reason though :( – Sriram Sakthivel Jul 31 '14 at 11:08
  • @SriramSakthivel After reading your answer the first time I had a strong impression it would work since the cursor is not bound to any specific control. It worked well and was simple, don't know why didn't occured to me. Have a good day. – Fábio Antunes Jul 31 '14 at 11:14
0

Let's take a simple example of dragging a button around.

Suppose you have two types of container controls: 1) an X-Y layout 2) a flow layout (assume left to right)

When you click to drag the button, record the x-offset and y-offset from the click to the top left corner of the control. As well, record the index of the control within the Controls collection.

As the mouse moves, first check if the mouse has changed container controls. If so, then remove the button from its current parent and add it to the new parent.

If the button is added to a flow control, then you need to calculate the new index. To do this, calculate the distance from the mouse to the closest edge of a bounding box of all other controls. Then if the mouse is left of the center of that control, insert to that control's index minus 1, otherwise insert to the right of that control (index + 1).

If the button is added to an X-Y layout, then the index doesn't matter that much. You can simple just set the button's location relative to the mouse plus the x-offset, and y-offset.

As the mouse is dragging, you will need to force the controls to refresh. I think calling Invalidate() on the container control should be sufficient.

This should give you the basic idea that you could use to start coding something.

Loathing
  • 5,109
  • 3
  • 24
  • 35
  • Yes I kinda implemented that within a single container where I could move controls and their positions would be updated according to where they were dropped. And if the control was dropped on top of another control, such control in the back would take the previous position of the control that was placed on top, basically a swap of positions... – Fábio Antunes Jul 31 '14 at 02:35
  • However when moving a control from one panel to another, you will notice the control "image" is cropped when this leaves the panel premises. I guess the only way is to add the control to the form and bring the control to front whenever I start moving it, and when the control is dropped (the mose no longer is holding it) drop it and add it on the appropriate container (found by the coordinates where the control is dropped). – Fábio Antunes Jul 31 '14 at 02:37