Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I drag file names from Windows Explorer and drop them into a ListBox?

Place a ListBox on your form, set its AllowDrop property and handle both DragEnter and DragDrop as below.

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

private void listBox1_DragDrop( object sender, DragEventArgs e )
{
  foreach ( string s in (string[]) e.Data.GetData( "FileDrop", false ) )
    listBox1.Items.Add( Path.GetFileName( s ) );
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter