Tricky Drag and Drop Question

D

David Hearn

I have the need to be able to drag and drop some text into a listbox or a
grid, doesn't really matter which one. I can use either. The tricky part
about it is that when the user drags and drops into the control, I need to
be able to let them drag it into a certain spot in the listbox or grid and
have the text dropped into that order. In other words, if the user drags and
drops the text between the first and second items already in the list, the
dropped item now becomes the second item and the second item moves down one
slot and then the program will save the items in the order that they were
dropped in.

Any way of accomplishing this?

Thanks in advance!
 
M

Munir Husseini

Am Thu, 2 Jun 2005 08:31:14 -0400 schrieb David Hearn:
I have the need to be able to drag and drop some text into a listbox or a
grid, doesn't really matter which one. I can use either. The tricky part
about it is that when the user drags and drops into the control, I need to
be able to let them drag it into a certain spot in the listbox or grid and
have the text dropped into that order. In other words, if the user drags and
drops the text between the first and second items already in the list, the
dropped item now becomes the second item and the second item moves down one
slot and then the program will save the items in the order that they were
dropped in.

Any way of accomplishing this?

Thanks in advance!

Hi David,

using a listbox's GetItemHeight method and Control's static MousePosition
property you should be able to calculate the index of the item the mouse is
hovering above. Then simply use ListBox.Items.Insert and you're done.

Hope this helps.

Regards,
Munir Husseini
 
S

Stoitcho Goutsev \(100\) [C# MVP]

You probably won't need the MousePosition since the cursor position comes
with each DragOver event. GetItemHeight won't do good job either because the
elements could have different heights.
The problem is not in the DnD the problem is whether the target control
allows hittesting agianst their items.
If you can do that implementation shouldn't be that difficult.

As a starter try out this piece of code. Add a listbox to the form, set its
AllowDrop property to *true* and hook its DragEnter, DragOver and DragDrop
events with the provided below handlers.

Then grab a text from some application that allow dragging away text (e.g.
WordPad or MSWord) and drop it over the list box. While you drag over the
list box it will track the mouse position by changing the selection.
If you drop over a list box item then the item is going to move one position
down and new item will be inserted. There is no way (not that I know) to
hittest against a position between two elements.

private void listBox1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.StringFormat))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void listBox1_DragOver(object sender,
System.Windows.Forms.DragEventArgs e)
{
Point pt = this.listBox1.PointToClient(new Point(e.X, e.Y));
int index = this.listBox1.IndexFromPoint(pt.X, pt.Y);
if(index != this.listBox1.SelectedIndex)
this.listBox1.SelectedIndex = index;
}

private void listBox1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
int selIndex = this.listBox1.SelectedIndex;
string data = (string)e.Data.GetData(DataFormats.StringFormat);
if(selIndex >= 0)
this.listBox1.Items.Insert(selIndex, data);
else
this.listBox1.Items.Add(data);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top