Results 1 to 4 of 4

Thread: DragDrop file onto your form

  1. #1

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    DragDrop file onto your form

    I have been working on an app that formats an XML file into a TreeView and thought this bit of the code might come in handy for you.
    This part enables you to drop an external file from your PC onto a TreeView (or any other control for that matter) where you can then do what you want with it. In my case just output the location.

    Here you are:

    1.
    Change the AllowDrop property of your control to True

    2.
    Select the little lightning bolt in the Properties window and double-click the DragDrop and DragEnter events to create them in your code. Or just create them manually of course

    3.
    In the DragEnter event put this code:
    Code:
    e.Effect = e.AllowedEffect;
    
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
       e.Effect = DragDropEffects.Move;
    }
    else 
    { 
       e.Effect = DragDropEffects.None; 
    }
    Obviously you can extras to the allowed DataFormats such as Text, RTF etc.

    4.
    In the DragDrop event add this code:
    Code:
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
    
        foreach (string fileName in fileNames)
        {
           TreeView1.Items.Add(fileName);
        }
    }

    Hey presto you got a tree view that you can drag files directly onto!
    Obviously if you change this to a Text you can drag text directly onto it

    Enjoy!
    Life is one big rock tune

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: DragDrop file onto your form

    Hey,

    You might want to think about asking for this thread to be moved to the Code Bank.

    Examples of code like this should be kept in there.

    Gary

  3. #3

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: DragDrop file onto your form

    Who would be the person to ask?
    Life is one big rock tune

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: DragDrop file onto your form

    Hey,

    If you PM one of the Mod's. either Mendhak, Rob, Kleinma, etc, they should be able to help you.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width