Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WindowsForms
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 at 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
0 OriginalGriff 210
1 kbrandwijk 100
2 Richard MacCutchan 100
3 ProgramFOX 80
4 Mukesh Bhagat 65
0 Sergey Alexandrovich Kryukov 9,050
1 OriginalGriff 8,151
2 CPallini 2,613
3 Richard MacCutchan 2,221
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 24 May 2012
Copyright © CodeProject, 1999-2014
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