1

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

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

例如,假设 FlowPanelLayout 包含 3 个控件,并且用户想要将第一个控件拖动到第三个控件位置。因此,用户单击并按住第一个 DataGridViewFilterSortElement,然后拖动到第二个控件上,这导致第二个控件移动到位置 1 of 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
4

1 回答 1

1

这个页面解释了如何做你想做的事。我试过了,看起来还不错。 http://www.vbdotnetforums.com/gui/45818-flowlayoutpanel-repositioning-object.html

于 2012-08-06T21:05:42.453 回答