PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Tricky Drag and Drop Question
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Tricky Drag and Drop Question
![]() |
Tricky Drag and Drop Question |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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! |
|
|
|
#2 |
|
Guest
Posts: n/a
|
I just achieved what you're trying to using TreeViewAdv from Syncfusion
Tools. http://www.syncfusion.com Regards. |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

