0

I have a drag drop operation in my flowLayoutPanel . Now when dragging some control, I want that control to be showed with mouse moving on mouse coordinates.

I have this in my main class

Panel dragAble = new Panel();

in my dragDrop event I have this

dragAble.Width = item.Width;
dragAble.Height = item.Height;
dragAble.Left = e.X;
dragAble.Top = e.Y;

Where I assign the new control's(which have to be showed ) Width and Height to dragging Width and Height. and setting it's coordinates to mouse coordinates.

I need to draw it on dragEnter event handler. How to show it on screen ?

It's the same in Windows, when dragging a file or folder, you can see it when dragging.

Romo Daneghyan
  • 2,099
  • 3
  • 18
  • 23
  • How about this? http://stackoverflow.com/questions/3868941/how-to-allow-user-to-drag-a-dynamically-created-control-at-the-location-of-his-c – mike1952 Apr 02 '14 at 14:04
  • I need something like this http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2012/04/mac_drag_drop1.png See when dragging with mouse u can see the dragable control too. – Romo Daneghyan Apr 02 '14 at 14:08
  • Do you need to be able to drag between forms or just over a single form? – mike1952 Apr 02 '14 at 14:09
  • Did you see [my answer to your previous nearly-identical question](http://stackoverflow.com/a/22787553/939213)? – ispiro Apr 02 '14 at 14:10
  • Yes ispiro, I saw, but could not use it :/ Mike, only on one form – Romo Daneghyan Apr 02 '14 at 14:11
  • Means did not understand what are u saying. – Romo Daneghyan Apr 02 '14 at 14:18
  • possible duplicate of [C# Drag-and-Drop: Show the dragged item while dragging](http://stackoverflow.com/questions/3240603/c-sharp-drag-and-drop-show-the-dragged-item-while-dragging) – LarsTech Apr 02 '14 at 15:02

2 Answers2

0

you can to handle this issue using the MouseDown,MouseMove,MouseUp events create a boolean flag like IsMouseDown=false on MouseDown set IsMouseDown to true on MouseMove check if flage is true then change the left and top properties dragAble.Left = e.X; dragAble.Top = e.Y; on MouseUp just set IsMouseDown to false

0

You need to combine a custom cursor like isipro recommended with the tutorial here http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=vs.110).aspx

It's a bit more work than you'd probably like, but it's also a reasonably complicated problem.

EDIT:
Here's some code that does what you ask for. It could be engineered better, but it gets the point across:

class Dragger
{
    private readonly Form _parent;
    private Panel _dragee;

    public Dragger(Form parent)
    {
        _parent = parent;
    }

    public void MouseMoved(object sender, MouseEventArgs e)
    {
        if (_dragee != null)
        {
            _dragee.Location = _parent.PointToClient(Cursor.Position);
        }
    }

    public void StartDragging(Panel panel)
    {
        _dragee = panel;
    }

    public void StopDragging()
    {
        _dragee = null;
    }
}


public partial class Form1 : Form
{
    private readonly Dragger _dragger;

    public Form1()
    {
        InitializeComponent();
        _dragger = new Dragger(this);
        panel1.MouseMove += _dragger.MouseMoved;
        panel1.MouseDown += panel1_MouseDown;
        panel1.MouseUp += panel1_MouseUp;
    }

    void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        _dragger.StopDragging();
    }

    void panel1_MouseDown(object sender, MouseEventArgs e)
    {

        _dragger.StartDragging((Panel)sender);
    }

}

Important notes: if you click the panel and drag it, the object itself is the thing which starts generating mousemove events. So you have to listen to them, not to the form itself. You'll also need to make sure that the dragged control is in front.

Good luck - and really, the custom cursor is the way to go :)

mike1952
  • 493
  • 5
  • 12
  • Means I have to make a new cursor ? Can't I just draw that control on the mouse position just Mike? – Romo Daneghyan Apr 02 '14 at 14:38
  • That's probably a bad idea - you'll have to draw the cursor on top, and it'll probably look pretty ugly. A custom cursor is the _right_ answer. Dynamically moving a control, while _an_ answer, will be more problematic. That said, I'll have a play and see what I can work out for a hack. – mike1952 Apr 02 '14 at 14:42
  • have you got anything new Mike ? – Romo Daneghyan Apr 02 '14 at 17:19
  • I'm glad. Please mark it as an answer if it has solved your problem :) – mike1952 Apr 03 '14 at 14:44