Click here to Skip to main content
12,294,775 members (59,148 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# windows-forms
Instead of loading a file with a:
OpenFileDialog ofd = new OpenFileDialog ();
How do I drag the file into a text box and text box then displays the file path?

Thank you,
KZ
Posted 24-May-12 4:19am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need to create a new class, based on a TextBox, and handle the Drag-and-drop:

public class MyTextBox : TextBox
    {
    public MyTextBox()
        {
        AllowDrop = true;
        Multiline = true;
        DragDrop += new DragEventHandler(MyTextBox_DragDrop);
        DragEnter += new DragEventHandler(MyTextBox_DragEnter);
        }
 
    void MyTextBox_DragEnter(object sender, DragEventArgs e)
        {
        e.Effect = DragDropEffects.Copy;
        }
 
    void MyTextBox_DragDrop(object sender, DragEventArgs e)
        {
        DataObject data = (DataObject)e.Data;
        if (data.ContainsFileDropList())
            {
            string[] rawFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (rawFiles != null)
                {
                List<string> lines = new List<string>();
                foreach (string path in rawFiles)
                    {
                    lines.AddRange(File.ReadAllLines(path));
                    }
                Lines = lines.ToArray();
                }
            }
        }
     }

[edit]
Oops! Went a little far there...That displays the file content, rather than path. Path is even easier: just replace
lines.AddRange(File.ReadAllLines(path));
With
lines.Add(path);
- OriginalGriff
[/edit]
  Permalink  
v2
Comments
Killzone DeathMan 24-May-12 11:20am
   
I already done it, it works :D
I forget about the event... :$

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160525.2 | Last Updated 24 May 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100