.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Drag and Drop text from one textbox to other

Posted By:Deepika Haridas       Posted Date: September 22, 2009    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

Windows Forms : Use Drag-and-Drop

This code will show you how to implement drag and drop functionality in a 
Windows Form. Drag-and-drop is one of the fundamental metaphors underlying the 
Microsoft Windows family of operating systems. Users understand that some 
items can be moved around by holding the mouse down on them, and that they'll 
get appropriate visual feedback when they're over a spot where the item can be 
dropped. They expect to be able to move data and images from one spot to 
another this way. You can control all aspects of the process, including which 
controls allow dragging, what data they make available to drag, and where it 
can be dropped. You can implement this both within a single application and 
between applications. 

Featured Highlights:

a) From a TextBox control to two other TextBox controls, one of which does not 
have the AllowDrop property set to True, demonstrating how to prevent a drop on 
a control.

The MouseDown event for the left TextBox. This event fires when the mouse is in the control's bounds and the mouse button is clicked.

Declare constant for use in detecting whether the Ctrl key was pressed during the drag operation.

const byte CtrlMask = 8;

private void TextBoxLeft_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
if (e.Button == MouseButtons.Left)
//invoke the drag and drop operation
txtLeft.DoDragDrop(txtLeft.SelectedText, DragDropEffects.Move | DragDropEffects.Copy);

// the DragDrop event for the lower right TextBox. This event fires
// when the mouse button is released, terminating the drag-and-drop operation.

private void TextBoxLowerRight_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
txtLowerRight.Text = e.Data.GetData(DataFormats.Text).ToString();
// if the Ctrl key was not pressed, remove the source text to effect a
// drag-and-drop move.

if ((e.KeyState & CtrlMask) != CtrlMask)
txtLeft.Text = "";

// the DragEnter event for the lower right TextBox. DragEnter is the
// event that fires when an object is dragged into the control's bounds.

private void TextBoxLowerRight_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
// Check to be sure that the drag content is the correct type for this
// control. if not, reject the drop.

if (e.Data.GetDataPresent(DataFormats.Text))
// if the Ctrl key was pressed during the drag operation then perform a Copy. if not, perform a Move.

if ((e.KeyState & CtrlMask) == CtrlMask)
e.Effect = DragDropEffects.Copy;
e.Effect = DragDropEffects.Move;
e.Effect = DragDropEffects.None;

For More Details visit my Blog : http://angeldeeps.blogspot.com


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend