PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Tricky Drag and Drop Question

Reply

Tricky Drag and Drop Question

 
Thread Tools Rate Thread
Old 02-06-2005, 01:31 PM   #1
David Hearn
Guest
 
Posts: n/a
Default Tricky Drag and Drop Question


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!


  Reply With Quote
Old 03-06-2005, 02:12 AM   #2
yuriy_zubarev
Guest
 
Posts: n/a
Default Re: Tricky Drag and Drop Question

I just achieved what you're trying to using TreeViewAdv from Syncfusion
Tools. http://www.syncfusion.com

Regards.

  Reply With Quote
Old 03-06-2005, 04:06 PM   #3
Munir Husseini
Guest
 
Posts: n/a
Default Re: Tricky Drag and Drop Question

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
  Reply With Quote
Old 04-06-2005, 01:51 AM   #4
Stoitcho Goutsev \(100\) [C# MVP]
Guest
 
Posts: n/a
Default Re: Tricky Drag and Drop Question

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);

}




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


"Munir Husseini" <mhusseini@icomcept.de> wrote in message
news:ymo4r9zvesfo$.1b1hxm5tcxg41$.dlg@40tude.net...
> 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



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off