Advertisement
Guest User

Untitled

a guest
Apr 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     '/*********************************************************************/
  2.    '/*                   SUB NAME:  tbcAATTabs_MouseDown              
  3.    '/*********************************************************************/
  4.    '/* WRITTEN BY:  Gina Mistura
  5.    '/* Date CREATED: March 28, 2019
  6.    '/*********************************************************************/
  7.     '/* Function() PURPOSE:                                  
  8.     '/*                                          
  9.    '/*      Initiates the drag and drop event only when the left mouse
  10.     '/*      button is clicked
  11.    '/*********************************************************************/
  12.     '/*  CALLED BY:                                        
  13.    '/*            
  14.    '/*********************************************************************/
  15.     '/*  CALLS:                                    
  16.    '/*            findHoveredTab
  17.    '/*
  18.    '/*********************************************************************/
  19.     '/*  PARAMETER LIST (In Parameter Order):                    
  20.     '/*                                            
  21.    '/*  sender: Sender of the argument
  22.     '/*  e: MouseEventArgs
  23.    '/*********************************************************************/
  24.     '/* SAMPLE INVOCATION:                             
  25.     '/*                                        
  26.    '/*********************************************************************/
  27.     '/*  LOCAL VARIABLE LIST (Alphabetically):                   
  28.     '/*     source: Tab that was clicked on
  29.    '/*********************************************************************/
  30.     '/* MODIFICATION HISTORY:                              
  31.     '/*                                          
  32.     '/*  WHO   WHEN     WHAT                                 
  33.    '/*  ---   ----     -------------------------------------------------
  34.     '/*          
  35.    '/*********************************************************************/
  36.  
  37.     Private Sub tbcAATTabs_MouseDown(sender As Object, e As MouseEventArgs) Handles tbcAATTabs.MouseDown
  38.         ' Drag events can only be initiated on left click events.
  39.        If e.Button <> MouseButtons.Left Then
  40.             Return
  41.         End If
  42.  
  43.         ' Figure out which tab was clicked on to use it as the data for the drag event.
  44.        Dim source = FindHoveredTab(False)
  45.  
  46.         ' Ensure that a tab header was selected and that it is not the "+" tab.
  47.        If source IsNot Nothing AndAlso tbcAATTabs.TabPages.IndexOf(source) <> tbcAATTabs.TabCount - 1 Then
  48.             tbcAATTabs.DoDragDrop(source, DragDropEffects.Move)
  49.         End If
  50.     End Sub
  51.  
  52.     '/*********************************************************************/
  53.    '/*                   SUB NAME:  tcbAATTabs_DragOver            
  54.    '/*********************************************************************/
  55.    '/* WRITTEN BY:  Gina Mistura
  56.    '/* Date CREATED: March 28, 2019
  57.    '/*********************************************************************/
  58.     '/* Function() PURPOSE:                                  
  59.     '/*                                          
  60.    '/*      Moves tab location on drag over event. Ensures tab placement
  61.     '/*      is allowed before continuing to drop event
  62.    '/*********************************************************************/
  63.     '/*  CALLED BY:                                        
  64.    '/*            
  65.    '/*********************************************************************/
  66.     '/*  CALLS:                                    
  67.    '/*            findHoveredTab
  68.    '/*
  69.    '/*********************************************************************/
  70.     '/*  PARAMETER LIST (In Parameter Order):                    
  71.     '/*                                            
  72.    '/*  sender: Sender of the argument
  73.     '/*  e: DragEventArgs
  74.    '/*********************************************************************/
  75.     '/* SAMPLE INVOCATION:                             
  76.     '/*                                        
  77.    '/*********************************************************************/
  78.     '/*  LOCAL VARIABLE LIST (Alphabetically):                   
  79.     '/*     target: Tab at current location the user wants to move the
  80.     '/*     tab that was clicked on to
  81.    '/*********************************************************************/
  82.     '/* MODIFICATION HISTORY:                              
  83.     '/*                                          
  84.     '/*  WHO   WHEN     WHAT                                 
  85.    '/*  ---   ----     -------------------------------------------------
  86.     '/*          
  87.    '/*********************************************************************/
  88.    Private Sub tcbAATTabs_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles tbcAATTabs.DragOver
  89.         ' Only perform swapping when TabPage data is present.
  90.        If Not e.Data.GetDataPresent(GetType(TabPage)) Then
  91.             Return
  92.         End If
  93.  
  94.         ' Indicate that a move operation is in progress.
  95.        e.Effect = DragDropEffects.Move
  96.  
  97.         Dim target = FindHoveredTab(True)
  98.         If target IsNot Nothing Then
  99.             Dim source = DirectCast(e.Data.GetData(GetType(TabPage)), TabPage)
  100.             If target IsNot source Then
  101.                 ' We know that source and target tabs are different so we can attempt to swap them.
  102.                Dim sourceIndex = tbcAATTabs.TabPages.IndexOf(source)
  103.                 Dim targetIndex = tbcAATTabs.TabPages.IndexOf(target)
  104.  
  105.                 ' Don't allow the "+" button to be swapped.
  106.                If targetIndex = tbcAATTabs.TabCount - 1 Then
  107.                     Return
  108.                 End If
  109.  
  110.                 ' The source and target tabs may not be contiguous so we need to step through each
  111.                ' of the tabs individually to swap them.
  112.                While sourceIndex <> targetIndex
  113.                     ' The index of the page to swap the source with.
  114.                    Dim nextIndex = sourceIndex + If(sourceIndex < targetIndex, 1, -1)
  115.  
  116.                     ' Swap the order of the two tabs.
  117.                    tbcAATTabs.TabPages(sourceIndex) = tbcAATTabs.TabPages(nextIndex)
  118.                     tbcAATTabs.TabPages(nextIndex) = source
  119.  
  120.                     ' Advance the sourceIndex to prepare for the next iteration.
  121.                    sourceIndex = nextIndex
  122.                 End While
  123.  
  124.                 ' Reselect the target tab after the swap occured.
  125.                tbcAATTabs.SelectedTab = source
  126.             End If
  127.         End If
  128.     End Sub
  129.  
  130.     ''' <summary>
  131.    ''' Searches for the tab that the mouse cursor is hovering over.
  132.    ''' </summary>
  133.    '''
  134.    ''' <param name="isDragging">Whether a drag event is in progress. If true then only the
  135.    ''' cursor's X coordinate will be considered when checking for a hover. If false, then
  136.    ''' the X and Y coordinates will be considered.</param>
  137.    '''
  138.    ''' <returns>The TabPage that the mouse cursor was over.</returns>
  139.  
  140.     '/*********************************************************************/
  141.    '/*                   SUB NAME:  FindHoveredTab            
  142.    '/*********************************************************************/
  143.    '/* WRITTEN BY:  Gina Mistura
  144.    '/* Date CREATED: March 28, 2019
  145.    '/*********************************************************************/
  146.     '/* Function() PURPOSE:                                  
  147.     '/*                                          
  148.    '/*      Used to determine tabs that are being dragged over, and tabs
  149.     '/*      whose indices will be changed after drag drop is completed
  150.    '/*********************************************************************/
  151.     '/*  CALLED BY:                                        
  152.    '/*             tcbAATTabs_DragOver and tbcAATTabs_MouseDown
  153.    '/*********************************************************************/
  154.     '/*  CALLS:                                    
  155.    '/*          
  156.    '/*
  157.    '/*********************************************************************/
  158.     '/*  PARAMETER LIST (In Parameter Order):                    
  159.     '/*                                            
  160.    '/*  isDragging: boolean to determine if drag event is in progress
  161.    '/*********************************************************************/
  162.     '/* SAMPLE INVOCATION:                             
  163.     '/*             Dim target = FindHoveredTab(True)                          
  164.    '/*********************************************************************/
  165.     '/*  LOCAL VARIABLE LIST (Alphabetically):                   
  166.     '/*     cursorPosition: Postion on cursor
  167.     '/*    
  168.    '/*********************************************************************/
  169.     '/* MODIFICATION HISTORY:                              
  170.     '/*                                          
  171.     '/*  WHO   WHEN     WHAT                                 
  172.    '/*  ---   ----     -------------------------------------------------
  173.     '/*          
  174.    '/*********************************************************************/
  175.    Private Function FindHoveredTab(isDragging As Boolean) As TabPage
  176.         Dim cursorPosition = tbcAATTabs.PointToClient(Cursor.Position)
  177.  
  178.         ' FIXME For some reason "Tab 1" does not seem to use correct bounds and only
  179.        ' allows swapping when the mouse is over the tab headers. This behavior
  180.        ' persists even after swapping. However, tabs that are swapped into the
  181.        ' first position do not display this behavior.
  182.  
  183.         For i = 0 To tbcAATTabs.TabCount - 1
  184.             Dim bounds = tbcAATTabs.GetTabRect(i)
  185.             If isDragging Then
  186.                 ' If a drag event is in progress we only care about the X coordinate of the
  187.                ' cursor. This allows the user to reorder tabs without having to keep their
  188.                ' cursor on the relatively small tab headers.
  189.                If bounds.Left <= cursorPosition.X And bounds.Right >= cursorPosition.X Then
  190.                     Return tbcAATTabs.TabPages(i)
  191.                 End If
  192.             Else
  193.                 ' If a drag event is not currently in progress then we need to make sure
  194.                ' the cursor is positioned over the tab header.
  195.                If bounds.Contains(cursorPosition) Then
  196.                     Return tbcAATTabs.TabPages(i)
  197.                 End If
  198.             End If
  199.         Next
  200.  
  201.         ' The cursor was not hovering over any tab.
  202.        Return Nothing
  203.     End Function
  204.  
  205. #End Region
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement