-
Feb 7th, 2023, 02:05 PM
#1
[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?
-
Feb 8th, 2023, 12:58 AM
#2
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|