Click here to Skip to main content
13,290,729 members (69,511 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 9 Aug 2007

Drag and Drop text containing file in textbox.

, 27 Sep 2007
Rate this:
Please Sign up or sign in to vote.
You can drag and drop text file or any other file which contains text in textbox.


In many of application we need drag and drop functionality; in that same context this article helps you to drag and drop text containing file in to text box. This is a simplest method which I found for text file drag and drop. In case of drag and drop Microsoft .NET has provided support by may properties to control, events and methods.

In this application you can drag and drop a file which may of any extension but contains text in it. For text container I used textbox. For performing drag and drop I handled DragEnter, DragDrop event of TextBox and the AllowDrop property.



This property is set to true for any control for which we want drag and drop support. This control may be a TextBox which holds data.



This event is fired whenever the mouse with a pressed left button will enter into a control's region. At that time, the control's DragEnter event is fired. Generally, we require to check the type of data available, which is done as follows.

private void txtFileContent_DragEnter(object sender, DragEventArgs e)
    // If file is dragged, show cursor "Drop allowed"
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;
    e.Effect = DragDropEffects.None;


This event is fired when the left mouse button is released by the user and the user intends to drop data on the target control. Generally, the target control accepts data here as follows.

private void txtFileContent_DragDrop(object sender, DragEventArgs e)
        Array a = (Array) e.Data.GetData(DataFormats.FileDrop);
        if(a != null)
            string s = a.GetValue(0).ToString();
    catch(Exception ex)
        MessageBox.Show("Error in DragDrop function: " + ex.Message);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Shinde Sachin
Technical Lead iGate, India
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
masouda6515-Jul-12 2:35
membermasouda6515-Jul-12 2:35 
GeneralThanks for the article Pin
Member 11122324-Aug-10 4:05
memberMember 11122324-Aug-10 4:05 
GeneralDragAllow property is AllowDrop Pin
JYX3-Sep-07 14:48
memberJYX3-Sep-07 14:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 27 Sep 2007
Article Copyright 2007 by Shinde Sachin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid