RSS Feed (xml)

Use a Drag-and-Drop Operation

A drag-and-drop operation allows the user to transfer information from one place to another by clicking an item and "dragging" it to another location. A drag-and-drop operation consists of the following three basic steps:

  1. The user clicks a control, holds the mouse button down, and begins dragging. If the control supports the drag-and-drop feature, it sets aside some information.

  2. The user drags the mouse over another control. If this control accepts the dragged type of content, the mouse cursor changes to the special drag-and-drop icon (arrow and page). Otherwise, the mouse cursor becomes a circle with a line drawn through it.

  3. When the user releases the mouse button, the data is sent to the control, which can then process it appropriately.

To support drag and drop, you must handle the DragEnter, DragDrop, and (typically) the MouseDown events. This example uses two text boxes. Here's the code needed to attach the event handlers we'll use:

this.TextBox2.MouseDown += new MouseEventHandler(this.TextBox_MouseDown);
this.TextBox2.DragDrop += new DragEventHandler(this.TextBox_DragDrop);
this.TextBox2.DragEnter += new DragEventHandler(this.TextBox_DragEnter);

this.TextBox1.MouseDown += new MouseEventHandler(this.TextBox_MouseDown);
this.TextBox1.DragDrop += new DragEventHandler(this.TextBox_DragDrop);
this.TextBox1.DragEnter += new DragEventHandler(this.TextBox_DragEnter);

To start a drag-and-drop operation, you call the source control's DoDragDrop method. At this point you submit the data and specify the type of operations that will be supported (copying, moving, and so on). The following recipe example initiates a drag-and-drop operation when the user clicks a text box:

private void TextBox_MouseDown(object sender, 
  System.Windows.Forms.MouseEventArgs e) {

    TextBox txt = (TextBox)sender;
    txt.DoDragDrop(txt.Text, DragDropEffects.Copy);

Controls that can receive dragged data must have the AllowDrop property set to true. These controls will receive a DragEnter event when the mouse drags the data over them. At this point, you can examine the data that's being dragged, decide whether the control can accept the drop, and set the DragEventArgs.Effect property accordingly, as shown here in this code:

private void TextBox_DragEnter(object sender, 
  System.Windows.Forms.DragEventArgs e) {

    if (e.Data.GetDataPresent(DataFormats.Text)) {
        e.Effect = DragDropEffects.Copy;
    else {
        e.Effect = DragDropEffects.None;

The final step is to respond to the DragDrop event, which occurs when the user releases the mouse button.

private void TextBox_DragDrop(object sender, 
  System.Windows.Forms.DragEventArgs e) {

    TextBox txt = (TextBox)sender;
    txt.Text = (string)e.Data.GetData(DataFormats.Text);

Using the code presented so far, you can create a simple drag-and-drop test application (see below figure) that allows text to be dragged from one text box to another. You can also drag text from another application and drop it into either text box.


Technorati :

No comments:

Post a Comment

Archives In Web World

Fun Mail - Fun in the Mail