Flowlayoutpanel and repositioning object in it.

kingballz

New member
Joined
Jan 13, 2011
Messages
4
Programming Experience
3-5
I'm going crazy trying to figure this one out...99.9% of the time the answer is on the net somewhere but this time I've not been able to find ANYTHING out. I found a page telling me how to do it in c# but I struggle to follow it.

Anyway, it seems simple but I have a flowlayoutpanel and a number of buttons in it...I basically want to be able to re-order the buttons by clicking and dragging them.

Does anyone have any ideas? It seems so simple yet I'm totally stumped.

Thanks for any help in advance guys,
 
Yes, drag-drop operations in their basic form is simple :p Here are some help topics about it:
Drag-and-Drop Functionality in Windows Forms
Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms

I wrote a code sample, and also included a custom drag cursor so it looks nicer, it uses a checkbox to determine if the buttons operates as normal or should be dragged.
VB.NET:
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For Each child As Control In Me.FlowLayoutPanel1.Controls
        AddHandler child.MouseDown, AddressOf childs_MouseDown
    Next
End Sub

Private dragcursor As Cursor, dragtype As Type

Private Sub childs_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If Me.ReorderCheckBox.Checked Then
        Dim source As Control = CType(sender, Control)
        Using bmp As New Bitmap(source.Width, source.Height)
            source.DrawToBitmap(bmp, New Rectangle(Point.Empty, source.Size))
            Me.dragcursor = New Cursor(bmp.GetHicon)
        End Using
        Me.dragtype = source.GetType
        Me.FlowLayoutPanel1.DoDragDrop(source, DragDropEffects.Move)
        Me.dragcursor.Dispose()
    End If
End Sub

Private Sub FlowLayoutPanel1_GiveFeedback(ByVal sender As Object, ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles FlowLayoutPanel1.GiveFeedback
    e.UseDefaultCursors = False
    Cursor.Current = Me.dragcursor
End Sub

Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragEnter
    If e.AllowedEffect = DragDropEffects.Move AndAlso e.Data.GetDataPresent(dragtype) Then
        e.Effect = DragDropEffects.Move
    End If
End Sub

Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
    Dim source As Control = CType(e.Data.GetData(dragtype), Control)
    Dim target As Control = Me.FlowLayoutPanel1.GetChildAtPoint(Me.FlowLayoutPanel1.PointToClient(New Point(e.X, e.Y)))
    If target IsNot Nothing Then
        Dim ix As Integer = Me.FlowLayoutPanel1.Controls.GetChildIndex(target)
        Me.FlowLayoutPanel1.Controls.SetChildIndex(source, ix)
    End If
End Sub
Remember to set AllowDrop to True on the FlowLayoutPanel.
 
Wow!!! Thanks for such a detailed reply. This was the finishing touch I needed to make my program look neater.

Thank so much for taking the time to post it. :D
 
Hi John,

I applied your code in my program and it works like charm.

However. the code seems not working if i add new control during runtime. Any clue on this?
 
Hi John,

I applied your code in my program and it works like charm.

However. the code seems not working if i add new control during runtime. Any clue on this?
You probably missed AddHandler for the new child control.
 
Back
Top