Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Drag Drop a file onto a list box?

Posted on 2005-03-04
5
Medium Priority
?
305 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:Tom Knowlton
5 Comments
 
LVL 6

Accepted Solution

by:
melodiesoflife earned 1700 total points
ID: 13464547
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
 
LVL 14

Assisted Solution

by:Thandava Vallepalli
Thandava Vallepalli earned 100 total points
ID: 13464773
0
 
LVL 22

Assisted Solution

by:Mohammed Nasman
Mohammed Nasman earned 100 total points
ID: 13465042
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
 
LVL 9

Assisted Solution

by:sun4sunday
sun4sunday earned 100 total points
ID: 13465194

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
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13465197
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question