locked
Drag and Drop controls on Form RRS feed

  • Question

  •  

    Hi,

     

    I apologize if this is already covered somewhere.  I did a search and couldn't find anything.

     

    I'm trying to have either a calendar, or multiple panels representing different job duties, where the user will drag a label, or some similar object onto the panel, thus scheduling by dragging and dropping.

     

    I've found tutorials for drag and drop in TreeViews, but I don't see how to drag a label, or any other control.  I see the AllowDrop property is there, but the ItemDrag event is not.

     

    Does anyone know how to go about this?

     

    Thank You!

    Wednesday, October 31, 2007 11:41 AM

Answers

  • ChritinaB,

     

    According to your question on drag and drop the controls in your Windows Forms application, I would like to provide you the suggestions as follows:

     

    1. Drag and Drop issue: Drag and drop is actually the same as cutting and pasting (or copying and pasting) using the mouse instead of the keyboard. In both cases you have a source (where you are cutting or copying from) and a target (where you are pasting to). During either operation, a copy of the data is maintained in memory. Cut and paste uses the Clipboard; drag and drop uses a DataObject object, which is in essence a private clipboard.

     

    Here is the sequence of events in a typical drag-and-drop operation:

     

    1). Dragging is initiated by calling the DoDragDrop method for the source control. The DoDragDrop method takes two parameters: data, specifying the data to pass allowedEffects, specifying which operations (copying and/or moving) are allowed. A new DataObject object is automatically created.

     

    2). This in turn raises the GiveFeedback event. In most cases you do not need to worry about the GiveFeedback event, but if you wanted to display a custom mouse pointer during the drag, this is where you would add your code.


    3). Any control with its AllowDrop property set to True is a potential drop target. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event.


    4). As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.


    5).If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.
    For most simple cases, you can enable dragging with a single line of code, and you can enable dropping with just a few more lines of code.

     

    For further information, please see the article: Implementing Drag and Drop in Visual Basic .NET

     

    2. To perform drag-and-drop operations within Windows-based applications you must handle a series of events, most notably the DragEnter, DragLeave, and DragDrop events. By working with the information available in the event arguments of these events, you can easily facilitate drag-and-drop operations. The following article provides you two examples on dragging and dropping data on the form:

     

    Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms

     

    3. ItemDrag event issue: ItemDrag event is used in TreeView and ListView control as you mentioned. I would like to provide you the example on the the ListView control:

     

    How to provide drag-and-drop functionality in Microsoft Windows applications by using Visual Basic 2005 or Visual Basic .NET

    You can use the ItemDrag event of the ListView control to set the data that you want to drag. When you call the DoDragDrop method for the ListView control, you initiate the dragging. The DoDragDrop method takes the data parameter and the allowedEffects parameter. The data parameter contains the data to be passed to the target ListView control. The allowedEffects parameter specifies what operations (copying, moving, or both) are permitted.

    Hope that can help you with this kind of problems.

    Friday, November 2, 2007 5:57 AM
  • Ok, so I'm not sure if this will work for regular objects, but you should be able to modify it to do so.

     

    I ended up changing the code examples a bit.  Basically, I have 3 TreeViews displayed in 3 different panels.  You can drag from InactiveEmployees and InactiveStations to CurrentSchedule.  You can drag from CurrentSchedule to either two treeViews, or to itself.  This is done by checking to see what node its drug to, and doing a cut paste type thing.  I'm sure that you could mod this out to work with objects.  Maybe look for the location point, and then test to see that it falls within the appropriate boundaries.  If it does, set it to the concrete location that you want it to snap to.

     

    I don't have time to work it out here, but I'd love to hear if this works for you or not.

     

    Good luck!

     

    Code Block

    Public Sub CurrentSchedule_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles CurrentSchedule.ItemDrag, InactiveEmployees.ItemDrag, InactiveStations.ItemDrag
            Dim data As New DataObject(DataFormats.Serializable, e.Item)
            sender.DoDragDrop(data, DragDropEffects.Move)
        End Sub

        Public Sub CurrentSchedule_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CurrentSchedule.DragOver, InactiveEmployees.DragOver, InactiveStations.DragOver
            If e.Data.GetDataPresent(DataFormats.Serializable) Then
                e.Effect = DragDropEffects.Move
                Dim mousepoint As New Point(e.X, e.Y)
                sender.SelectedNode = sender.GetNodeAt(sender.PointToClient(mousepoint))
            End If
        End Sub

        Public Sub CurrentSchedule_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CurrentSchedule.DragDrop, InactiveEmployees.DragDrop, InactiveStations.DragDrop
            Dim DestinationNode As TreeNode
            Dim pt As Point
            pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
            DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
            If DestinationNode Is Nothing Then
                MsgBox("Please select a position for this entry.")
            Else
                If e.Data.GetDataPresent(DataFormats.Serializable) Then
                    sender.Nodes.Remove(e.Data.GetData(DataFormats.Serializable, True))
                    sender.SelectedNode.Nodes.Add(e.Data.GetData(DataFormats.Serializable, True))
                End If
            End If
        End Sub

     

     

    Monday, December 10, 2007 1:51 PM

All replies

  • ChritinaB,

     

    According to your question on drag and drop the controls in your Windows Forms application, I would like to provide you the suggestions as follows:

     

    1. Drag and Drop issue: Drag and drop is actually the same as cutting and pasting (or copying and pasting) using the mouse instead of the keyboard. In both cases you have a source (where you are cutting or copying from) and a target (where you are pasting to). During either operation, a copy of the data is maintained in memory. Cut and paste uses the Clipboard; drag and drop uses a DataObject object, which is in essence a private clipboard.

     

    Here is the sequence of events in a typical drag-and-drop operation:

     

    1). Dragging is initiated by calling the DoDragDrop method for the source control. The DoDragDrop method takes two parameters: data, specifying the data to pass allowedEffects, specifying which operations (copying and/or moving) are allowed. A new DataObject object is automatically created.

     

    2). This in turn raises the GiveFeedback event. In most cases you do not need to worry about the GiveFeedback event, but if you wanted to display a custom mouse pointer during the drag, this is where you would add your code.


    3). Any control with its AllowDrop property set to True is a potential drop target. The AllowDrop property can be set in the Properties window at design time, or programmatically in the Form_Load event.


    4). As the mouse passes over each control, the DragEnter event for that control is raised. The GetDataPresent method is used to make sure that the format of the data is appropriate to the target control, and the Effect property is used to display the appropriate mouse pointer.


    5).If the user releases the mouse button over a valid drop target, the DragDrop event is raised. Code in the DragDrop event handler extracts the data from the DataObject object and displays it in the target control.
    For most simple cases, you can enable dragging with a single line of code, and you can enable dropping with just a few more lines of code.

     

    For further information, please see the article: Implementing Drag and Drop in Visual Basic .NET

     

    2. To perform drag-and-drop operations within Windows-based applications you must handle a series of events, most notably the DragEnter, DragLeave, and DragDrop events. By working with the information available in the event arguments of these events, you can easily facilitate drag-and-drop operations. The following article provides you two examples on dragging and dropping data on the form:

     

    Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms

     

    3. ItemDrag event issue: ItemDrag event is used in TreeView and ListView control as you mentioned. I would like to provide you the example on the the ListView control:

     

    How to provide drag-and-drop functionality in Microsoft Windows applications by using Visual Basic 2005 or Visual Basic .NET

    You can use the ItemDrag event of the ListView control to set the data that you want to drag. When you call the DoDragDrop method for the ListView control, you initiate the dragging. The DoDragDrop method takes the data parameter and the allowedEffects parameter. The data parameter contains the data to be passed to the target ListView control. The allowedEffects parameter specifies what operations (copying, moving, or both) are permitted.

    Hope that can help you with this kind of problems.

    Friday, November 2, 2007 5:57 AM
  • Thank you.  This is all great information,and I'm sure I'll be able to do it now.

     

    Thanks again!

    Friday, November 2, 2007 11:34 AM
  • Hi,

     

    I reviwed the Drag-Drop entry you posted.It sure works out with data drag-drop.But my question is how can I do drag-drop for objects.For example I have a Button on the form, I want to replace it to another location on form by drag-drop.

     

    Saturday, December 8, 2007 11:11 AM
  • Ok, so I'm not sure if this will work for regular objects, but you should be able to modify it to do so.

     

    I ended up changing the code examples a bit.  Basically, I have 3 TreeViews displayed in 3 different panels.  You can drag from InactiveEmployees and InactiveStations to CurrentSchedule.  You can drag from CurrentSchedule to either two treeViews, or to itself.  This is done by checking to see what node its drug to, and doing a cut paste type thing.  I'm sure that you could mod this out to work with objects.  Maybe look for the location point, and then test to see that it falls within the appropriate boundaries.  If it does, set it to the concrete location that you want it to snap to.

     

    I don't have time to work it out here, but I'd love to hear if this works for you or not.

     

    Good luck!

     

    Code Block

    Public Sub CurrentSchedule_ItemDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles CurrentSchedule.ItemDrag, InactiveEmployees.ItemDrag, InactiveStations.ItemDrag
            Dim data As New DataObject(DataFormats.Serializable, e.Item)
            sender.DoDragDrop(data, DragDropEffects.Move)
        End Sub

        Public Sub CurrentSchedule_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CurrentSchedule.DragOver, InactiveEmployees.DragOver, InactiveStations.DragOver
            If e.Data.GetDataPresent(DataFormats.Serializable) Then
                e.Effect = DragDropEffects.Move
                Dim mousepoint As New Point(e.X, e.Y)
                sender.SelectedNode = sender.GetNodeAt(sender.PointToClient(mousepoint))
            End If
        End Sub

        Public Sub CurrentSchedule_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles CurrentSchedule.DragDrop, InactiveEmployees.DragDrop, InactiveStations.DragDrop
            Dim DestinationNode As TreeNode
            Dim pt As Point
            pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
            DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
            If DestinationNode Is Nothing Then
                MsgBox("Please select a position for this entry.")
            Else
                If e.Data.GetDataPresent(DataFormats.Serializable) Then
                    sender.Nodes.Remove(e.Data.GetData(DataFormats.Serializable, True))
                    sender.SelectedNode.Nodes.Add(e.Data.GetData(DataFormats.Serializable, True))
                End If
            End If
        End Sub

     

     

    Monday, December 10, 2007 1:51 PM
  • if anyone needs a c# version that i wrote here it is:

        Point pLabelPrecision = new Point(0,0);
        
        private void label8_MouseDown(object sender, MouseEventArgs e)
        {     
                pLabelPrecision.X = e.X;
          pLabelPrecision.Y = e.Y;
    
                    label8.BackColor = System.Drawing.Color.DarkRed;
        }
        private void label8_MouseMove(object sender, MouseEventArgs e)
        {
               if (drag)
          {
            // render the animation of the control being dragged.
            int newX = e.X + label8.Location.X - pLabelPrecision.X;
            int newY = e.Y + label8.Location.Y - pLabelPrecision.Y;
            Point pLabel = new Point(newX, newY);
    
            label8.Location = pLabel;
    
          }
        }
    
        private void label8_MouseUp(object sender, MouseEventArgs e)
        {
    
                if (drag)
          {   
            // backcolor changed to signify the label has been dropped.
            label8.BackColor = System.Drawing.Color.Red;
    
                    int newX = e.X + label8.Location.X - pLabelPrecision.X;
            int newY = e.Y + label8.Location.Y - pLabelPrecision.Y;
            Point pLabel = new Point(newX, newY);
    
            // changes the location
            label8.Location = pLabel;
          }
    
          // exits the dragstate
          drag = false;
        }
      }
    
    hope this helps!
    Monday, August 9, 2010 7:58 PM