Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag a file into my listview ang get the path
 

Drag a file into my listview ang get the path

Hi all!!

Having a bit of a problem. Im trying to drag a file into my listview and it will add only the path to the file.

Anyone with a good solution??

would help me allot!!

André Dani  Thursday, May 29, 2008 8:07 AM
You are not initializing the ListViewItem correctly. I'm guessing you want something like this:

private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}

private void listView1_DragDrop(object sender, DragEventArgs e)
{
string[] filenames = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string file in filenames)
{
ListViewItem item = new ListViewItem();
item.Tag = file;
item.Text = System.IO.Path.GetFileNameWithoutExtension(file);
listView1.Items.Add(item);
}
}

nobugz  Thursday, May 29, 2008 5:14 PM
One question per thread please. Several problems, you can only start on DoDragDrop() at a time. And you're only dragging a string. Here's the way to do it:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Copy);
}

private void listView2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) != null)
e.Effect = DragDropEffects.Copy;
}

private void listView2_DragDrop(object sender, DragEventArgs e) {
ListView.SelectedListViewItemCollection coll =
(ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
foreach (ListViewItem item in coll)
listView2.Items.Add((ListViewItem)item.Clone());
}
nobugz  Friday, May 30, 2008 12:05 AM
Getting the path to the file is good. What else do you want to get? Post some code if you can't explain the problem properly.
nobugz  Thursday, May 29, 2008 2:11 PM

My code look like this:

private void listView1_DragDrop(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop))

{

String[] filenames = e.Data.GetData(DataFormats.FileDrop) as String[];

if (filenames != null)

{

listView1.Enabled = true;

ListViewItem item = new ListViewItem(new string[] {filenames });

listView1.Items.Add(item);

}

}

}

private void listView1_DragEnter(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(DataFormats.FileDrop))

{

string[] filenames = e.Data.GetData(DataFormats.FileDrop) as string[];

if (filenames != null)

{

e.Effect = DragDropEffects.Copy;

}

else

{

e.Effect = DragDropEffects.None;

}

}

else

{

e.Effect = DragDropEffects.None;

}

}

but the path does´t show in the listview. it displays "System" something.

What do i do wrong?

Tnx!

André Dani  Thursday, May 29, 2008 2:32 PM
You are not initializing the ListViewItem correctly. I'm guessing you want something like this:

private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}

private void listView1_DragDrop(object sender, DragEventArgs e)
{
string[] filenames = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string file in filenames)
{
ListViewItem item = new ListViewItem();
item.Tag = file;
item.Text = System.IO.Path.GetFileNameWithoutExtension(file);
listView1.Items.Add(item);
}
}

nobugz  Thursday, May 29, 2008 5:14 PM

Thank you! Works just fine!!

When we are talking about drag and drop function i can explain my other problem.

I´m trying to make a drag and drop function beetween listviews. I just wanna copy the item to the other, dont remove. And the copy fuction doesn´t bring the subitems,

private void listView1_MouseDown(object sender, MouseEventArgs e)

foreach (ListViewItem SelectedName in listView1.SelectedItems)

DragDropEffects.Move);

private void listView2_DragDrop(object sender, DragEventArgs e)

DataFormats.Text).ToString());

private void listView2_DragEnter(object sender, DragEventArgs e)

if (e.Data.GetDataPresent(DataFormats.Text))

DragDropEffects.Move;

else

DragDropEffects.None;

One question per thread please. Several problems, you can only start on DoDragDrop() at a time. And you're only dragging a string. Here's the way to do it:

private void listView1_ItemDrag(object sender, ItemDragEventArgs e) {
listView1.DoDragDrop(listView1.SelectedItems, DragDropEffects.Copy);
}

private void listView2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetData(typeof(ListView.SelectedListViewItemCollection)) != null)
e.Effect = DragDropEffects.Copy;
}

private void listView2_DragDrop(object sender, DragEventArgs e) {
ListView.SelectedListViewItemCollection coll =
(ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));
foreach (ListViewItem item in coll)
listView2.Items.Add((ListViewItem)item.Clone());
}
nobugz  Friday, May 30, 2008 12:05 AM

You can use google to search for other answers

Custom Search

More Threads

• An unhandled exception of type 'System.NullReferenceException'
• InstallShield
• dirty system.drawing.systemicons
• how to pass value from treeview selectednode
• How do you create multiple combobox's at runtime? i know how to do single but not multi
• Ordering of categories in propertygrid
• Inventory Database FIFO
• How to expand text box automatically
• How to manually "click" a button on a ToolStrip
• DataGridView - Delete Rows when Bound to typed List (List(Of ...) List<>)