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
DragDrop event of TextBox and the
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)
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();
MessageBox.Show("Error in DragDrop function: " + ex.Message);