Tech Off Thread

5 posts

Drag and Drop between user controls

Back to Forum: Tech Off
  • User profile image
    Mauricio Feijo

    I had a hard time finding any information at all about dragging and dropping between controls of different types contained in separate user controls. I am talking about dragging from an Infragistics UltraGrid in a user control to an Infragistics UltraWinTree in another user control, for example. Probably a common scenario, and I bet a lot of people go trough the same trouble, so maybe this can help.

    The magic happens in a few steps:

    First you need to set the AllowDrop property to true on both controls.

    Next you need to catch the SelectionDrag event. It will fire when you start dragging from the grid. ON that handler you need to call the DoDragDrop method. That will start the process and change your cursor for you, based on the DFragDropEffect you use on the method call. I used DragDropEffect.Move, and that changes the cursor to a little plus sign inside a box.

    private void utgGrid_SelectionDrag( object sender, CancelEventArgs e)

    {

    this.DoDragDrop(((UltraGrid)sender).Selected.Rows[0].Cells["ItemID"].Text, DragDropEffects.Copy);

    }



    Now we are waiting for the user to move the mouse over the tree.  We will capture that on the DragEnter event of the tree, check to see if what is being dragged is what we want and set e.Effect to DragDropEffects.Copy if we want to allow the drop or to DragDropEffects.None if we don't want to allow the drop.

    private void utrTree_DragEnter(object sender, DragEventArgs e)

    {

    {

    if (e.Data.GetDataPresent(DataFormats.StringFormat))

    {

       e.Effect = DragDropEffects.Copy;

    }

    else

    {

       e.Effect = DragDropEffects.None;

    }

    }

    }

    On that handler we will check to see if what is being dragged is what we want, and set e.Effect to DragDropEffects.Copy if we care for it or to DragDropEffects.None if we don't want to allow the drop.

    Finally we need to catch the drop on the DragDrop event of the tree.

    private void utrTree_DragDrop(object sender, DragEventArgs e)

    {

    //Below you call whatever method you want to process the drop, include the item on the tree  or whatever you need to do.

    ProcessDrop((String)e.Data.GetData(DataFormats.StringFormat), utrTree.GetNodeFromPoint(utrTree.PointToClient(new Point(e.X,e.Y))));

    }

    See ya!

    Mauricio Feijo
    www.mauriciofeijo.com

  • User profile image
    littleguru

    Isn't this normal drag and drop between two controls?

  • User profile image
    footballism

       Good move, Mauricio, this info is really helpful, sharing knowledge is not a bad thing actually:P

    Keep on good work!

    Sheva

  • User profile image
    maxence51

    This is exactly what I needed!

    Thank you Mauricio.

  • User profile image
    spivonious

    littleguru said:
    Isn't this normal drag and drop between two controls?

    Looks like normal drag-and-drop to me, too. This is all very clearly explained in the MSDN.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.