Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag from C# from and drop in other application
 

Drag from C# from and drop in other application

Hi
My problem is that I have to Drag a word file from c# treeview and then drop in MSWORD. Any idea how can i do this?
Please help me :(
  • Moved byKarel ZikmundMSFTThursday, September 24, 2009 5:42 PMWinForms Q (From:Building Development and Diagnostic Tools for .Net)
  •  
emmadzahid  Thursday, September 24, 2009 10:41 AM
Hi,

You want to drag files from a tree view in C# and open them up in Microsoft Word?

If so try this. You will basically need to create a new DataObject and for all selected files in the treeview, add them to this DataObject using the SetFileDropList method. You will need to wrap up the filenames in a StringCollection which is passed to the SetFileDropList method.

Then start the drag and drop using the treeview DoDragDrop method.


// need to use StringCollection
using System.Collections.Specialized;


// NOTE:  attach the event (if not using Designer to do it...)
// this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);

// implement the MouseDown event to start drag and drop in tree view
private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {	   
            // this will test to see which node you have the mouse on - you can mod this to get all selected nodes        
            // we are assuming that the text of the node is the filename, for example C:\Test.doc.
            TreeViewHitTestInfo tvhti = this.treeView1.HitTest(e.Location);
            TreeNode tn = tvhti.Node;           
            // now wrap up the file(s) in a DataObject for Word to understand
            DataObject obj = new DataObject();
            StringCollection sc = new StringCollection();
            sc.Add(tn.Text);
            obj.SetFileDropList(sc);            
            // start the drag and drop
            this.treeView1.DoDragDrop(obj, DragDropEffects.All);
           
        }







Andez
Andez  Thursday, September 24, 2009 1:59 PM
Thanks :)
I have done this in a bit different way

private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {	   
            TreeViewHitTestInfo tvhti = this.treeView1.HitTest(e.Location);
            TreeNode tn = tvhti.Node;           
          
	 DataObject obj = new DataObject(DataFormat.Filedrop, "C:\\Test.doc"); // IMPORTANT LINE
            this.treeView1.DoDragDrop(obj, DragDropEffects.All);
           
        }

emmadzahid  Friday, September 25, 2009 5:10 PM
Hi,

You want to drag files from a tree view in C# and open them up in Microsoft Word?

If so try this. You will basically need to create a new DataObject and for all selected files in the treeview, add them to this DataObject using the SetFileDropList method. You will need to wrap up the filenames in a StringCollection which is passed to the SetFileDropList method.

Then start the drag and drop using the treeview DoDragDrop method.


// need to use StringCollection
using System.Collections.Specialized;


// NOTE:  attach the event (if not using Designer to do it...)
// this.treeView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseDown);

// implement the MouseDown event to start drag and drop in tree view
private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {	   
            // this will test to see which node you have the mouse on - you can mod this to get all selected nodes        
            // we are assuming that the text of the node is the filename, for example C:\Test.doc.
            TreeViewHitTestInfo tvhti = this.treeView1.HitTest(e.Location);
            TreeNode tn = tvhti.Node;           
            // now wrap up the file(s) in a DataObject for Word to understand
            DataObject obj = new DataObject();
            StringCollection sc = new StringCollection();
            sc.Add(tn.Text);
            obj.SetFileDropList(sc);            
            // start the drag and drop
            this.treeView1.DoDragDrop(obj, DragDropEffects.All);
           
        }







Andez
Andez  Thursday, September 24, 2009 1:59 PM
Thanks :)
I have done this in a bit different way

private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {	   
            TreeViewHitTestInfo tvhti = this.treeView1.HitTest(e.Location);
            TreeNode tn = tvhti.Node;           
          
	 DataObject obj = new DataObject(DataFormat.Filedrop, "C:\\Test.doc"); // IMPORTANT LINE
            this.treeView1.DoDragDrop(obj, DragDropEffects.All);
           
        }

emmadzahid  Friday, September 25, 2009 5:10 PM

You can use google to search for other answers

Custom Search

More Threads

• Login Functionality Help
• Balloon tip in .net
• Form.Invoke speed
• TabPage BackColor
• eps
• Design-Time Attribute for a Font Array in Visual Studio 2005
• Enable one Textbox, Disable the other in a Vb.net Winform
• Return UNC from a Mapped drive.
• Unbound DataGridView with Image Column and Text Columns
• visual C#, form 2