Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '/*********************************************************************/
- '/* SUB NAME: tbcAATTabs_MouseDown
- '/*********************************************************************/
- '/* WRITTEN BY: Gina Mistura
- '/* Date CREATED: March 28, 2019
- '/*********************************************************************/
- '/* Function() PURPOSE:
- '/*
- '/* Initiates the drag and drop event only when the left mouse
- '/* button is clicked
- '/*********************************************************************/
- '/* CALLED BY:
- '/*
- '/*********************************************************************/
- '/* CALLS:
- '/* findHoveredTab
- '/*
- '/*********************************************************************/
- '/* PARAMETER LIST (In Parameter Order):
- '/*
- '/* sender: Sender of the argument
- '/* e: MouseEventArgs
- '/*********************************************************************/
- '/* SAMPLE INVOCATION:
- '/*
- '/*********************************************************************/
- '/* LOCAL VARIABLE LIST (Alphabetically):
- '/* source: Tab that was clicked on
- '/*********************************************************************/
- '/* MODIFICATION HISTORY:
- '/*
- '/* WHO WHEN WHAT
- '/* --- ---- -------------------------------------------------
- '/*
- '/*********************************************************************/
- Private Sub tbcAATTabs_MouseDown(sender As Object, e As MouseEventArgs) Handles tbcAATTabs.MouseDown
- ' Drag events can only be initiated on left click events.
- If e.Button <> MouseButtons.Left Then
- Return
- End If
- ' Figure out which tab was clicked on to use it as the data for the drag event.
- Dim source = FindHoveredTab(False)
- ' Ensure that a tab header was selected and that it is not the "+" tab.
- If source IsNot Nothing AndAlso tbcAATTabs.TabPages.IndexOf(source) <> tbcAATTabs.TabCount - 1 Then
- tbcAATTabs.DoDragDrop(source, DragDropEffects.Move)
- End If
- End Sub
- '/*********************************************************************/
- '/* SUB NAME: tcbAATTabs_DragOver
- '/*********************************************************************/
- '/* WRITTEN BY: Gina Mistura
- '/* Date CREATED: March 28, 2019
- '/*********************************************************************/
- '/* Function() PURPOSE:
- '/*
- '/* Moves tab location on drag over event. Ensures tab placement
- '/* is allowed before continuing to drop event
- '/*********************************************************************/
- '/* CALLED BY:
- '/*
- '/*********************************************************************/
- '/* CALLS:
- '/* findHoveredTab
- '/*
- '/*********************************************************************/
- '/* PARAMETER LIST (In Parameter Order):
- '/*
- '/* sender: Sender of the argument
- '/* e: DragEventArgs
- '/*********************************************************************/
- '/* SAMPLE INVOCATION:
- '/*
- '/*********************************************************************/
- '/* LOCAL VARIABLE LIST (Alphabetically):
- '/* target: Tab at current location the user wants to move the
- '/* tab that was clicked on to
- '/*********************************************************************/
- '/* MODIFICATION HISTORY:
- '/*
- '/* WHO WHEN WHAT
- '/* --- ---- -------------------------------------------------
- '/*
- '/*********************************************************************/
- Private Sub tcbAATTabs_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles tbcAATTabs.DragOver
- ' Only perform swapping when TabPage data is present.
- If Not e.Data.GetDataPresent(GetType(TabPage)) Then
- Return
- End If
- ' Indicate that a move operation is in progress.
- e.Effect = DragDropEffects.Move
- Dim target = FindHoveredTab(True)
- If target IsNot Nothing Then
- Dim source = DirectCast(e.Data.GetData(GetType(TabPage)), TabPage)
- If target IsNot source Then
- ' We know that source and target tabs are different so we can attempt to swap them.
- Dim sourceIndex = tbcAATTabs.TabPages.IndexOf(source)
- Dim targetIndex = tbcAATTabs.TabPages.IndexOf(target)
- ' Don't allow the "+" button to be swapped.
- If targetIndex = tbcAATTabs.TabCount - 1 Then
- Return
- End If
- ' The source and target tabs may not be contiguous so we need to step through each
- ' of the tabs individually to swap them.
- While sourceIndex <> targetIndex
- ' The index of the page to swap the source with.
- Dim nextIndex = sourceIndex + If(sourceIndex < targetIndex, 1, -1)
- ' Swap the order of the two tabs.
- tbcAATTabs.TabPages(sourceIndex) = tbcAATTabs.TabPages(nextIndex)
- tbcAATTabs.TabPages(nextIndex) = source
- ' Advance the sourceIndex to prepare for the next iteration.
- sourceIndex = nextIndex
- End While
- ' Reselect the target tab after the swap occured.
- tbcAATTabs.SelectedTab = source
- End If
- End If
- End Sub
- ''' <summary>
- ''' Searches for the tab that the mouse cursor is hovering over.
- ''' </summary>
- '''
- ''' <param name="isDragging">Whether a drag event is in progress. If true then only the
- ''' cursor's X coordinate will be considered when checking for a hover. If false, then
- ''' the X and Y coordinates will be considered.</param>
- '''
- ''' <returns>The TabPage that the mouse cursor was over.</returns>
- '/*********************************************************************/
- '/* SUB NAME: FindHoveredTab
- '/*********************************************************************/
- '/* WRITTEN BY: Gina Mistura
- '/* Date CREATED: March 28, 2019
- '/*********************************************************************/
- '/* Function() PURPOSE:
- '/*
- '/* Used to determine tabs that are being dragged over, and tabs
- '/* whose indices will be changed after drag drop is completed
- '/*********************************************************************/
- '/* CALLED BY:
- '/* tcbAATTabs_DragOver and tbcAATTabs_MouseDown
- '/*********************************************************************/
- '/* CALLS:
- '/*
- '/*
- '/*********************************************************************/
- '/* PARAMETER LIST (In Parameter Order):
- '/*
- '/* isDragging: boolean to determine if drag event is in progress
- '/*********************************************************************/
- '/* SAMPLE INVOCATION:
- '/* Dim target = FindHoveredTab(True)
- '/*********************************************************************/
- '/* LOCAL VARIABLE LIST (Alphabetically):
- '/* cursorPosition: Postion on cursor
- '/*
- '/*********************************************************************/
- '/* MODIFICATION HISTORY:
- '/*
- '/* WHO WHEN WHAT
- '/* --- ---- -------------------------------------------------
- '/*
- '/*********************************************************************/
- Private Function FindHoveredTab(isDragging As Boolean) As TabPage
- Dim cursorPosition = tbcAATTabs.PointToClient(Cursor.Position)
- ' FIXME For some reason "Tab 1" does not seem to use correct bounds and only
- ' allows swapping when the mouse is over the tab headers. This behavior
- ' persists even after swapping. However, tabs that are swapped into the
- ' first position do not display this behavior.
- For i = 0 To tbcAATTabs.TabCount - 1
- Dim bounds = tbcAATTabs.GetTabRect(i)
- If isDragging Then
- ' If a drag event is in progress we only care about the X coordinate of the
- ' cursor. This allows the user to reorder tabs without having to keep their
- ' cursor on the relatively small tab headers.
- If bounds.Left <= cursorPosition.X And bounds.Right >= cursorPosition.X Then
- Return tbcAATTabs.TabPages(i)
- End If
- Else
- ' If a drag event is not currently in progress then we need to make sure
- ' the cursor is positioned over the tab header.
- If bounds.Contains(cursorPosition) Then
- Return tbcAATTabs.TabPages(i)
- End If
- End If
- Next
- ' The cursor was not hovering over any tab.
- Return Nothing
- End Function
- #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement