Results 1 to 2 of 2

Thread: [RESOLVED] Inconsistent Reordering in FlowLayoutPanel

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,254

    Resolved [RESOLVED] Inconsistent Reordering in FlowLayoutPanel

    I have the a custom control that holds and displays information about cameras. These controls are added to a FlowLayoutPanel.

    Inside my control, I am handling the DragOver and MouseDown event of the control as well as some of the child controls:
    Code:
    Private Sub CameraPreview_DragOver(sender As Object, e As DragEventArgs) Handles Me.DragOver, LabelCameraInformation.DragOver, LabelCounterFail.DragOver, LabelCounterPass.DragOver, LabelCounterTotal.DragOver, LabelIpAddress.DragOver, PanelCameraContainer.DragDrop, StatusStripCounters.DragDrop, StatusStripHeader.DragDrop
        Dim dragOverCameraPreview = GetParentCameraPreview(sender)
        Dim parentFlowLayoutPanel = TryCast(dragOverCameraPreview.Parent, FlowLayoutPanel)
        If (parentFlowLayoutPanel Is Nothing) Then
            Return
        End If
    
        Dim draggedObject = e.Data.GetData(GetType(CameraPreview))
        If (draggedObject IsNot Nothing) Then
            Dim myIndex = parentFlowLayoutPanel.Controls.GetChildIndex(dragOverCameraPreview)
            Dim draggedCameraPreview = TryCast(draggedObject, CameraPreview)
            If (draggedCameraPreview IsNot Nothing) Then
                parentFlowLayoutPanel.Controls.SetChildIndex(draggedCameraPreview, myIndex)
            End If
        End If
    End Sub
    
    Private Sub CameraPreview_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown, LabelCameraInformation.MouseDown, LabelCounterFail.MouseDown, LabelCounterPass.MouseDown, LabelCounterTotal.MouseDown, LabelIpAddress.MouseDown, StatusStripHeader.MouseDown
        If (Not Parent.AllowDrop) Then
            Return
        End If
        DoDragDrop(Me, DragDropEffects.All)
    End Sub
    
    Private Function GetParentCameraPreview(sender As Object) As CameraPreview
        Dim self = TryCast(sender, CameraPreview)
        If (self IsNot Nothing) Then
            Return self
        End If
    
        Dim parent = DirectCast(sender, Control).Parent
        If (parent Is Nothing) Then
            Throw New ArgumentException("The control is not a child of a CameraPreview", NameOf(sender))
        End If
    
        Return GetParentCameraPreview(parent)
    End Function
    This will successfully reorder the controls in the FlowLayoutPanel, but my issue is that the behavior is very inconsistent.

    What I mean by that is that sometimes I will need to hold down the mouse and drag over control several times before the reordering actually takes place.

    Unfortunately, I cannot setup a breakpoint in my DragOver event handler because doing so breaks the "flow".

    Is there a better way of doing this?
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,254

    Re: Inconsistent Reordering in FlowLayoutPanel

    I was able to get a smooth effect by doing this:
    Code:
    Private Sub CameraPreview_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown, {every child MouseDown}
        If (e.Button <> MouseButtons.Left) Then
            Return
        End If
    
        Dim mouseDownCameraPreview = GetParentCameraPreview(sender)
        Dim parentFlowLayoutPanel = TryCast(mouseDownCameraPreview.Parent, FlowLayoutPanel)
        If (parentFlowLayoutPanel Is Nothing) Then
            Return
        End If
    
        parentFlowLayoutPanel.Tag = mouseDownCameraPreview
    End Sub
    
    Private Sub CameraPreview_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove, {every child MouseMove}
        Dim mouseMoveCameraPreview = GetParentCameraPreview(sender)
        If (mouseMoveCameraPreview Is Nothing) Then
            Return
        End If
    
        Dim parentFlowLayoutPanel = TryCast(mouseMoveCameraPreview.Parent, FlowLayoutPanel)
        If (parentFlowLayoutPanel Is Nothing OrElse parentFlowLayoutPanel.Tag Is Nothing) Then
            Return
        End If
    
        Dim cursorPosition = Cursor.Position
        Dim relativeCursorPosition = parentFlowLayoutPanel.PointToClient(cursorPosition)
        Dim controlUnderCursor = parentFlowLayoutPanel.GetChildAtPoint(relativeCursorPosition)
    
        If (controlUnderCursor Is Nothing) Then
            Return
        End If
    
        Dim cameraPreviewMoving = TryCast(parentFlowLayoutPanel.Tag, CameraPreview)
        If (cameraPreviewMoving Is Nothing OrElse cameraPreviewMoving Is controlUnderCursor) Then
            Return
        End If
    
        Dim targetIndex = parentFlowLayoutPanel.Controls.GetChildIndex(controlUnderCursor)
        parentFlowLayoutPanel.Controls.SetChildIndex(cameraPreviewMoving, targetIndex)
    End Sub
    
    Private Sub CameraPreview_MouseUp(sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp, {every child MouseUp}
        Dim mouseMoveCameraPreview = GetParentCameraPreview(sender)
        If (mouseMoveCameraPreview Is Nothing) Then
            Return
        End If
    
        Dim parentFlowLayoutPanel = TryCast(mouseMoveCameraPreview.Parent, FlowLayoutPanel)
        If (parentFlowLayoutPanel Is Nothing OrElse parentFlowLayoutPanel.Tag Is Nothing) Then
            Return
        End If
    
        parentFlowLayoutPanel.Tag = Nothing
    End Sub
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width