AMH
AMH

Reputation: 6451

Drag Drop doesn't work

in windows form I have Listview and FlowLayoutPanel

I want to drag from the Listview to FlowLayoutPanel so

in listview I use the DragEnter event

private void listViewGUI_DragEnter(object sender, DragEventArgs e)
        {
}

and in the FlowLayoutPanel I activate the DragDrop

private void fpnlDisplayedGUI_DragDrop(object sender, DragEventArgs e)
        {
}

the problem is that it doesn't work non of them enter any of this events , any idea how to make them enter am I missing any property

Best regards

Upvotes: 0

Views: 626

Answers (2)

Hans Passant
Hans Passant

Reputation: 942267

You put the DragEnter event on the wrong control, you must use the panel's. I think I know how you got into this trouble, ListView doesn't have any event that indicates the user started dragging an item. You'll need to synthesize that yourself. The basic approach is to record the mouse down position and use the MouseMove event to check if the user has moved the mouse far enough to start a drag. Like this:

    private Point dragMousePos;

    private void listView1_MouseDown(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) dragMousePos = e.Location;
    }

    private void listView1_MouseMove(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            int dx = Math.Abs(e.X - dragMousePos.X);
            int dy = Math.Abs(e.Y - dragMousePos.Y);
            if (dx >= SystemInformation.DoubleClickSize.Width ||
                dy >= SystemInformation.DoubleClickSize.Height) {
                var item = listView1.GetItemAt(dragMousePos.X, dragMousePos.Y);
                if (item != null) listView1.DoDragDrop(item, DragDropEffects.Move);
            }
        }
    }

Upvotes: 2

ispiro
ispiro

Reputation: 27713

The following is a simple example to show the basics of what you need:

public Form1()
{
    InitializeComponent();

    panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
    panel2.AllowDrop = true;
    panel2.DragEnter += new DragEventHandler(panel2_DragEnter);
    panel2.DragDrop += new DragEventHandler(panel2_DragDrop);
}

void panel2_DragDrop(object sender, DragEventArgs e)
{
    //handle the drop here.
}

void panel2_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

void panel1_MouseDown(object sender, MouseEventArgs e)
{
    panel1.DoDragDrop("whatever you want draged.", DragDropEffects.Move);
}

Upvotes: 2

Related Questions