Dragdrop for files . txt Windows Forms

Asked

Viewed 412 times

3

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

I am using version 4.6 of . NET Framework.

  • I wanted to drag you into one TextBox and show the contents of the file?

  • For a Listbox, however, it is a complete file, in format . txt.

  • It is a file with data to fill a ListBox? Or you want to fill only with the file name? If you want to fill in the contents of ListBox add the contents of the file to question.

  • Simply read the file to the end and play line by line in Listbox.

  • @Jonathanbarcela In general lines you want to put in a listbox the name of the text file and the number of lines it has. See if you can get help https://msdn.microsoft.com/pt-br/library/ezwyzy7b.aspx

1 answer

1

Jonathan, I imagine you want to get the address from the file. 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 the file is released 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 it’s 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-c/

  • 1

    Guy try to answer by showing the code snippet , so you answered it is not allowed here on sopt. in that case you can use the .

  • I have a Code Much like the one that Guilherme posted and it works this way and I think it answers the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.