c# – DragDrop para arquivos .txt Windows Forms

Question:

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

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

Answer:

Jonathan, I imagine you want to get the address from the file. Well, for that you need to 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 the file is dropped inside the TextBox (DragDrop Event) you create a string with the file

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

In short this is good luck.

Source: http://www.andrealveslima.com.br/blog/index.php/2015/03/11/implementando-drag-and-drop-arrastar-e-soltar-em-aplicacoes-windows-forms-com- ç/

Scroll to Top