DragDrop for Windows Forms .txt files

3

I would like to know if you can use Drag and Drop in Windows Forms for files outside the application, in this case a .txt (just like in WPF). I just found examples using ListBox. If it is possible, how to get the data from the file dragged?

I'm using version 4.6 of the .NET Framework.

    
asked by anonymous 22.06.2015 / 21:34

1 answer

1

Jonathan, I imagine you want to get the file address. Well, for this you need to do a check if what is being dragged is in fact a file with the DragOver Event

private void TextBox_DragOver(object sender, DragEventArgs e)
{
 if (e.Data.GetDataPresent(DataFormats.FileDrop))
    e.Effect = DragDropEffects.Link;
 else
    e.Effect = DragDropEffects.None;
}

Then when you drop the file inside the TextBox (DragDrop Event) you create a string with the

private void TextBox_DragDrop(object sender, DragEventArgs e)
{
  string arquivo = e.Data.GetData(DataFormats.FileDrop) as string;
  TextBox.Text = arquivo;
}

In short, that's good luck.

Source:   -com-c / "> link

    
22.03.2016 / 14:46