Ryan Hennings
Ryan Hennings

Reputation: 11

Move FlowLayoutPanel Controls via DragDrop

I have a FlowPanelLayout that can contain several UserControls called DataGridViewFilterSortElement. These controls look kind of like buttons, but different. I want the user to be able to click one of the DataGridViewFilterSortElement controls and drag it to another position (index) in the FlowLayoutPanel.

Is there a way to see the control physically moving as the user drags it to another position? In other words, is there a way to take a "snap shot" of the control that is being dragged (instead of a shadowed box) which would show the actual control moving as the cursor moves? Also, as the control is being dragged I'd like to have the other controls position to shift automatically instead of waiting for the user to drop the drag to see the shifts.

For example, let's say the FlowPanelLayout contains 3 controls and the user wants to drag the first control to the third controls position. So the user clicks and holds the first DataGridViewFilterSortElement, then drags over the second control, which causes the second control to shift to position 1 of 3, then the user drags over the third control, which causes the third control to shift to position 2 of 3, then the user drops the control in position 3. Is this possible? The little code I do have is below.

Here is a short little video that shows what I want to do: http://www.youtube.com/watch?v=YhyTni6KH0Q

    Private Sub lblDescription_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown, lblDescription.MouseDown
    ' if the user left clicks and holds the element begin a DragDrop action
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Me.DoDragDrop(Me, DragDropEffects.Move)
    End If
End Sub

Private Sub SortFlowLayoutPanel_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
    e.Effect = DragDropEffects.Move
End Sub

Private Sub SortFlowLayoutPanel_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SortFlowLayoutPanel.DragDrop

        If e.Data.GetData(GetType(DataGridViewFilterSortElement)) IsNot Nothing Then

            'Current Position           
            Dim myIndex As Integer = Me.SortFlowLayoutPanel.Controls.GetChildIndex(CType(e.Data.GetData(GetType(DataGridViewFilterSortElement)), DataGridViewFilterSortElement))

            'Dragged to control to location of next picturebox
            Dim element As DataGridViewFilterSortElement = CType(e.Data.GetData(GetType(DataGridViewFilterSortElement)), DataGridViewFilterSortElement)

            Me.SortFlowLayoutPanel.Controls.SetChildIndex(element, myIndex)
        End If

End Sub

Private Sub SortFlowLayoutPanel_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles SortFlowLayoutPanel.DragOver
    e.Effect = DragDropEffects.Move
End Sub

Upvotes: 1

Views: 5495

Answers (1)

DontFretBrett
DontFretBrett

Reputation: 1165

This page explains how to do what you want. I tried it, looks pretty good. http://www.vbdotnetforums.com/gui/45818-flowlayoutpanel-repositioning-object.html

Upvotes: 1

Related Questions