Move controls when Drag and drop on panel in C#

后端 未结 2 1251
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 13:37

I want to drag controls on panel and when dragging I want to move the control and get its location to drop on to panel. I have tried out mouseUp, mouseDown, MouseMove events

相关标签:
2条回答
  • 2020-12-10 14:25

    If your control is already on the panel and you're simply moving it within the same panel, then using the Mouse events is probably the easiest way to do this. My understanding is that Drag and Drop is more about conveying data between controls or even applications. Drag and drop would be a good fit if you're trying to allow a control to transfer between panels, for example.


    If you want to do both, then here's one possible idea:

    1. Perform move dragging within the same panel using your Mouse events.

    2. When you get a MouseLeave event on the panel, begin a DragDrop operation (some examples here) You can either remove the control from the panel or add some sort of 'gray out' effect to indicate that the control may be leaving.

    3. Handle the DragDrop on your target panel and place the control at the mouse location of the drop.

    This combines the intuitive feel of dragging the control around, while also providing a way to drag 'past' the panel and on to a new surface.

    0 讨论(0)
  • 2020-12-10 14:32

    You will need to use the mouse up and mouse down events to toggle your drag state. When you go mouse down, you start dragging. You record the relative position of the mouse within your control and the relative position of your control within the panel. You then follow the mouse as it moves and repositioning the control's top and left relative to the original location of the mouse within your control.

    0 讨论(0)
提交回复
热议问题