Drag Drop a file onto a list box?

Can someone provide code for this?



I have this C# Windows Application running.  The form has a list box on it.


I want to be able to drag the icon for a text file ONTO the list box  (from the Desktop or any other window that is open with text files listed) and the file will open and the LINES of text will be added one by one to the ListBox control using it's Items.Add(  )   method.



Thanks!


500 points for your trouble....




Tom
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

 private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
     {
          if (e.Data.GetDataPresent(DataFormats.FileDrop))
               e.Effect = DragDropEffects.All;
          else
               e.Effect = DragDropEffects.None;
     }
 
 private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
     {
          string[] files = (string[])e.Data.GetData("FileDrop", false);
          foreach (string s in files)
          {
               //just filename
               listBox1.Items.Add(s.Substring(1 + s.LastIndexOf(@"\")));
               //or fullpathname
               //     listBox1.Items.Add(s);
               // If you want to add the text in the drag file, please read it line by line and add to the list box here.
          }
     }
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mohammed NasmanSoftware DeveloperCommented:
Hello

  You need to use WM_DropFiles message with DragQueryFile and DragAcceptFiles API, I did that for win32 using Delphi, I will try to convert the code if I have sometime, or try to google on that

HTH

Regards,
Mohammed
0
sun4sundayCommented:

How to implement simple drag and drop functionality
http://www.thecodeproject.com/csharp/dragdropharsha.asp

Drag and Drop files from Windows Explorer to Windows Form
http://www.codeproject.com/csharp/dragdrop.asp

sun4sunday
0
Mohammed NasmanSoftware DeveloperCommented:
Ok, you could do it using .Net way without needs of API, here's my code

            private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
            {                   
                  foreach (string fileName in
                        (string[])e.Data.GetData(DataFormats.FileDrop) )
                  {
                        listBox1.Items.Add( fileName );
                  }            
            }
                  
            private void listBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
            {
                  if (e.Data.GetDataPresent(DataFormats.FileDrop))
                        e.Effect = DragDropEffects.Copy;
                  else
                        e.Effect = DragDropEffects.None;
            }

Just don't forget to make the "AllowDrop" property of ListBox to true

Regards,
Mohammed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.