Consider using the Clipboard class. It features all the methods necessary for putting data on the Windows clipboard and to retrieve data from the Windows clipboard.
StringCollection paths = new StringCollection();
paths.Add("f:\\temp\\test.txt");
paths.Add("f:\\temp\\test2.txt");
Clipboard.SetFileDropList(paths);
The code above will put the files test.txt and test2.txt for copy on the Windows Clipboard. After executing the code you can navigate to any folder and Paste (Ctrl+V) the files. This is equivalent to selecting both files in Windows Explorer and selecting copy (Ctrl+C).
If you are only copying and pasting within your application, you can map the cut/copy operation of your treeview to a method that just clones your selected node. Ie:
TreeNode selectedNode;
TreeNode copiedNode;
selectedNode = yourTreeview.SelectedNode;
if (selectedNode != null)
{
copiedNode = selectedNode.Clone;
}
// Then you can do whatever you like with copiedNode elsewhere in your app.
If you are wanting to be able to paste to other applications, then you'll have to use the clipboard. You can get a bit fancier than just plain text by learning more about the IDataObject interface. I can't remember the source but here's something I had in my own notes:
When implemented in a class, the IDataObject methods allow the user to store data in multiple formats in an instance of the class. Storing data in more than one format increases the chance that a target application, whose format requirements you might not know, can retrieve the stored data. To store data in an instance of IDataObject, call the SetData method and specify the data format in the format parameter. Set the autoConvert parameter to false if you do not want stored data to be converted to another format when it is retrieved. Invoke SetData multiple times on one instance of IDataObject to store data in more than one format.
Once you've populated an object that implements IDataObject (e.g. something called yourTreeNodeDataObject), then you can call:
Clipboard.SetDataObjecT(yourTreeNodeDataObject);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With