I have a small file explorer application [WINFORMS], and i use ListView Control to explore items.
So the listView display the files and the folders from the current address [From local Computer].
i need to enable drag&drop functionality to let a File/Folder easy to move/copy to another Folder in the same Address.
each item has some preperties:
"13.45 MB"
and for a folder, it's going to be string.Empty
[However, there's several way to know whether its a file or a folder].
I've seen many tutorials about how to use drag&drop in listview, but it was like from Windows File Explorer to ListView, or from a ListView to another one, but in my case i need to know how to drag&drop in the same ListView.
I've Enabled AllowDrop property of the ListView. and also Activated the following functions:
private void listView1_DragDrop(object sender, DragEventArgs e)
{
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
}
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
}
UPDATE:
i tried to use this :
private void listView1_DragDrop(object sender, DragEventArgs e)
{
ListViewItem item = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); //this should be the target item (FOLDER)
StringBuilder s = new StringBuilder();
foreach (ListViewItem i in listView1.SelectedItems)
{
s.AppendLine(i.Text);
}
MessageBox.Show("DRAGGED ITEM : " + s.ToString() + "TARGET ITEM : " + item.Text);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
var item = listView1.GetItemAt(e.X, e.Y);
if (item != null)
{
listView1.DoDragDrop(item, DragDropEffects.Copy);
}
}
at listView1_MouseDown
i get the item which mouse points onto, but it gives me the item before i drop the dragged item, so if i am dragging a folder named "SWImg" to the folder "ODDFiles" , the messageBox shows "SWImg - SWImg"
then i replaced listView1_MouseDown
with listView1_ItemDrag
:
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
if (e.Item != null)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Copy);
}
}
same result :S.