DragDrop file onto your form
I have been working on an app that formats an XML file into a TreeView and thought this bit of the code might come in handy for you.
This part enables you to drop an external file from your PC onto a TreeView (or any other control for that matter) where you can then do what you want with it. In my case just output the location.
Here you are:
1.
Change the AllowDrop property of your control to True
2.
Select the little lightning bolt in the Properties window and double-click the DragDrop and DragEnter events to create them in your code. Or just create them manually of course
3.
In the DragEnter event put this code:
Code:
e.Effect = e.AllowedEffect;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
Obviously you can extras to the allowed DataFormats such as Text, RTF etc.
4.
In the DragDrop event add this code:
Code:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string fileName in fileNames)
{
TreeView1.Items.Add(fileName);
}
}
Hey presto you got a tree view that you can drag files directly onto!
Obviously if you change this to a Text you can drag text directly onto it
Enjoy! :D
Re: DragDrop file onto your form
Hey,
You might want to think about asking for this thread to be moved to the Code Bank.
Examples of code like this should be kept in there.
Gary
Re: DragDrop file onto your form
Who would be the person to ask?
Re: DragDrop file onto your form
Hey,
If you PM one of the Mod's. either Mendhak, Rob, Kleinma, etc, they should be able to help you.
Gary