?
Solved

Drag Drop a file onto a list box?

Posted on 2005-03-04
5
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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