Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drag and Drop Files

Status
Not open for further replies.

randall2nd

Programmer
May 30, 2001
112
US
I am new to C# and .NET.
I have created an app that will list files in a listview control for specified directories.

I need to be able to do drag and drop file opens. Exactly like Windows Explorer.

For example:
If a text file (abc.txt) is in the listview, I need to be able to grab that entry and drag it onto an open notepad application, drop it and have note pad open the dropped file.

Thus far I have been able to grab the item drag it, but when I drag oustide my app the cursor turns to the not-allowed cursor.

What type of Object should I be using in DoDragDrop()?
How do create that Object?
What type(s) of Effects should I be using in DoDragDrop()?

Am I missing something?

All advice/guidance/examples are appreciated.


Randall2nd
 
Figured it out!!!!
Here is what worked for me.
Code:
private void lsvFiles_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
  //Build the string array of selected File names
  String[] sDragDropItem = new String[(e.Item as ListViewItem).ListView.SelectedItems.Count];
  for(int z=0; z < (e.Item as ListViewItem).ListView.SelectedItems.Count; z++)
  {
    sDragDropItem[z] = (e.Item as ListViewItem).ListView.SelectedItems[z].Text;
  }
      
  //Convert String Array into IDataObject of format FileDrop
  IDataObject tempDataObject = new DataObject(DataFormats.FileDrop,sDragDropItem);

  //I only want to drag on left mouse button click
  if (e.Button.Equals(MouseButtons.Left))
  {
    //make sure the ListView displays which rows are selected
    (e.Item as ListViewItem).ListView.Focus();
    //Start Drag Drop operation
    //only allow copy and link actions
    (e.Item as ListViewItem).ListView.DoDragDrop(tempDataObject,DragDropEffects.Copy|DragDropEffects.Link);
  }
}
[code]

Randall2nd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top