通过DragDrop移动FlowLayoutPanel控件

时间:2012-04-24 21:17:06

标签: drag-and-drop flowlayoutpanel

我有一个FlowPanelLayout,它可以包含几个名为DataGridViewFilterSortElement的UserControl。这些控件看起来有点像按钮,但不同。我希望用户能够单击其中一个DataGridViewFilterSortElement控件并将其拖动到FlowLayoutPanel中的另一个位置(索引)。

当用户将控制器拖到另一个位置时,有没有办法看到控制器在物理上移动?换句话说,有没有办法对被拖动的控件(而不是阴影框)进行“快照”,这将显示实际控件在光标移动时移动?此外,当拖动控件时,我希望让其他控件位置自动移动,而不是等待用户放下拖动以查看移位。

例如,假设FlowPanelLayout包含3个控件,用户想要将第一个控件拖动到第三个控件位置。因此,用户单击并保持第一个DataGridViewFilterSortElement,然后拖动第二个控件,这会导致第二个控件转移到位置1的3,然后用户拖动第三个控件,这会导致第三个控件转移到位置2 3,然后用户将控制器放在位置3.这可能吗?我所拥有的小代码如下。

这是一段简短的视频,展示了我想要做的事情: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

1 个答案:

答案 0 :(得分:1)

此页面介绍了如何做您想做的事情。我试了一下,看起来还不错。 http://www.vbdotnetforums.com/gui/45818-flowlayoutpanel-repositioning-object.html