Drag and drop within list, windows 8 app? - c#

Is there any good examples/tutorials on how to implement drag and drop within a window 8 C# list (listview, listbox …) out there?
What I would like Is a editable “Iphone-list”-experience, where I easily can rearrange items within a list. But I mostly find WinJS examples and I would like to have a c# example for win 8

Firstly You must enable the AllowDragDrop property.
Then write 3 events:
private void myList_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Link);
}
private void myList_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Link;
}
private void myList_DragDrop(object sender, DragEventArgs e)
{
// do whatever you need to reorder the list.
}
To get index of dropped item:
Point cp = myList.PointToClient(new Point(e.X, e.Y));
ListViewItem dragToItem = myList.GetItemAt(cp.X, cp.Y);
int dropIndex = dragToItem.Index;

If you need to drop onto a ListView or GridView, have the Drop event fire on the DataTemplate for the actual Item, not the whole list. Then you can tell which item it is dropped on.

Related

Is it possible to drag and drop an item into a specific position in a listbox?

I have three listboxes in a Windows Form. The first one will remain the same. I want to drag and drop items from the third one into the second one in order to match it with a specific element of the first one. So for example the first listbox has 10 elements, second is empty, third has 10 elements. I want to drag the second element of the third listbox into the fifth position in the second one (that was empty), so it matches with the fifht element of the first listbox. That makes sense? Can it be done? How? (not asking for the entire code, just want to know if it is possible, and may be some tips on how to do it)
Thanks!
Well, i don't know if it is the best way to do it, but it's working. I adapted a code for reordering a listbox with drag and drop.
private void LstBox3_MouseDown(object sender, MouseEventArgs e)
{
if (lstBox3.SelectedItem == null) return;
lstBox2.DoDragDrop(lstBox3.SelectedItem, DragDropEffects.All);
}
private void LstBox2_DragDrop(object sender, DragEventArgs e)
{
Point point = lstBox2.PointToClient(new Point(e.X, e.Y));
int index = lstBox2.IndexFromPoint(point);
if (index < 0) index = lstBox2.Items.Count - 1;
object data1 = lstBox3.SelectedItem;
lstBox2.Items.RemoveAt(index);
lstBox2.Items.Insert(index, data1);
}
private void LstBox2_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}

How to drag & drop items in the same ListView?

Consider this is a ListView that shows files and folders, I have already wrote code for copy/move/rename/show properties ...etc and I just need one more last thing. how to drag and drop in the same ListView like in Windows Explorer, I have move and copy functions, and I just need to get the items which user drops in some folder or in other way I need to get these two parameters to call copy function
void copy(ListViewItem [] droppedItems, string destination path)
{
// Copy target to destination
}
Start by setting the list view's AllowDrop property to true. Implementing the ItemDrag event to detect the start of a drag. I'll use a private variable to ensure that D+D only works inside of the control:
bool privateDrag;
private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
privateDrag = true;
DoDragDrop(e.Item, DragDropEffects.Copy);
privateDrag = false;
}
Next you'll need the DragEnter event, it will fire immediately:
private void listView1_DragEnter(object sender, DragEventArgs e) {
if (privateDrag) e.Effect = e.AllowedEffect;
}
Next you'll want to be selective about what item the user can drop on. That requires the DragOver event and checking which item is being hovered. You'll need to distinguish items that represent a folder from regular 'file' items. One way you can do so is by using the ListViewItem.Tag property. You could for example set it to the path of the folder. Making this code work:
private void listView1_DragOver(object sender, DragEventArgs e) {
var pos = listView1.PointToClient(new Point(e.X, e.Y));
var hit = listView1.HitTest(pos);
if (hit.Item != null && hit.Item.Tag != null) {
var dragItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
copy(dragItem, (string)hit.Item.Tag);
}
}
If you want to support dragging multiple items then make your drag object the ListView.SelectedIndices property.

How can I implement drag-and-drop to allow rearranging items in a ListView?

I would like to enable support for drag-and-drop in a ListView so that the user can rearrange the items, similar to what they can do in Windows Explorer.
Specifically, how do I enable the Drag event handler when I double-click on the ListView ?
This is what I get after double-clicking on the ListView :
private void listView1(object sender, EventArgs e)
However, I want it to be:
private void listView(object sender, DragEventArgs e)
How can I do this?
I have tried many ways, such as:
private void Form_Load(object sender, EventArgs e)
{
// Enable drag and drop for this form
// (this can also be applied to any controls)
this.AllowDrop = true;
// Add event handlers for the drag & drop functionality
this.DragEnter += new DragEventHandler(Form_DragEnter);
this.DragDrop += new DragEventHandler(Form_DragDrop);
}
But none of these seem to work.
You need to implement the DragEnter event and set the Effect property of the DragEventArgs. The DragEnter event is what allows things to be dropped into a control. After that the DragDrop event will fire when the mouse button is released.
Here is a version that will allow objects to be dropped into the a ListView:
private void Form1_Load(object sender, EventArgs e)
{
listView1.AllowDrop = true;
listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
}
void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void listView1_DragDrop(object sender, DragEventArgs e)
{
listView1.Items.Add(e.Data.ToString());
}
No doubt your sample code was taken from : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.allowdrop(v=vs.71).aspx
To answer your question: There is no built in functionality for dragging and dropping items within a ListView control. Even the MSDN documentation instructs you to implement your own code-behind for the various events in order to achieve this functionality (see the ListViewInsertionMark Class)
ObjectListView (an open source wrapper around .NET WinForms ListView) provides this ability without further work (plus lots of other nice features). Have a look at the "Drag and Drop" tab of the demo.
(source: codeproject.com)

Is it possible to have drag and drop from a ListView to a TreeView in Winforms?

If it's not possible, I can also use 2 TreeView controls. I just won't have a hierarchy in the second TreeView control. It's gonna act like some sort of repository.
Any code sample or tutorial would be very helpful.
ListView does not support drag-and-drop naturally, but you can enable it with a small bit of code:
http://support.microsoft.com/kb/822483
Here's an example that specifically does drag-and-drop from a ListView to a TreeView (it's an Experts Exchange link, so just wait a few seconds and then scroll to the bottom, where you'll find the answers):
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_22675010.html
Update: Code from the link:
Create a listview and a treeview. ( In my example, the listview is called listView1 and the treeview is called tvMain )
On the treeview, set AllowDrop to true.
Create an ItemDrag event on the listview
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Copy);
}
In this example items from the listview are copied to the 'drop' object.
Now, create a DragEnter event on the treeview:
private void tvMain_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
This was easy. Now the hard part starts. The following code adds the selected (and dragged) listview items to an existing node (make sure you have at least one node already in your treeview or the example will fail!)
Create a DragDrop event on the treeview:
private void tvMain_DragDrop(object sender, DragEventArgs e)
{
TreeNode n;
if (e.Data.GetDataPresent("System.Windows.Forms.ListView+SelectedListViewItemCollection", false))
{
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode dn = ((TreeView)sender).GetNodeAt(pt);
ListView.SelectedListViewItemCollection lvi = (ListView.SelectedListViewItemCollection)e.Data.GetData("System.Windows.Forms.ListView+SelectedListViewItemCollection");
foreach (ListViewItem item in lvi)
{
n = new TreeNode(item.Text);
n.Tag = item;
dn.Nodes.Add((TreeNode)n.Clone());
dn.Expand();
n.Remove();
}
}
}
To change the cursor while dragging, you have to create a GiveFeedback event for the ListView control:
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
if (e.Effect == DragDropEffects.Copy)
{
Cursor.Current = new Cursor(#"myfile.ico");
}
}
myfile.ico should be in the same directory as the .exe file.
This is just a simple example. You can extend it any way you like.

ListView, is there a simple way to allow dragging of items internally (built-in)? [duplicate]

I would like to enable support for drag-and-drop in a ListView so that the user can rearrange the items, similar to what they can do in Windows Explorer.
Specifically, how do I enable the Drag event handler when I double-click on the ListView ?
This is what I get after double-clicking on the ListView :
private void listView1(object sender, EventArgs e)
However, I want it to be:
private void listView(object sender, DragEventArgs e)
How can I do this?
I have tried many ways, such as:
private void Form_Load(object sender, EventArgs e)
{
// Enable drag and drop for this form
// (this can also be applied to any controls)
this.AllowDrop = true;
// Add event handlers for the drag & drop functionality
this.DragEnter += new DragEventHandler(Form_DragEnter);
this.DragDrop += new DragEventHandler(Form_DragDrop);
}
But none of these seem to work.
You need to implement the DragEnter event and set the Effect property of the DragEventArgs. The DragEnter event is what allows things to be dropped into a control. After that the DragDrop event will fire when the mouse button is released.
Here is a version that will allow objects to be dropped into the a ListView:
private void Form1_Load(object sender, EventArgs e)
{
listView1.AllowDrop = true;
listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
}
void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
void listView1_DragDrop(object sender, DragEventArgs e)
{
listView1.Items.Add(e.Data.ToString());
}
No doubt your sample code was taken from : http://msdn.microsoft.com/en-us/library/system.windows.forms.control.allowdrop(v=vs.71).aspx
To answer your question: There is no built in functionality for dragging and dropping items within a ListView control. Even the MSDN documentation instructs you to implement your own code-behind for the various events in order to achieve this functionality (see the ListViewInsertionMark Class)
ObjectListView (an open source wrapper around .NET WinForms ListView) provides this ability without further work (plus lots of other nice features). Have a look at the "Drag and Drop" tab of the demo.
(source: codeproject.com)

Categories