Search code examples
vb.nettabcontroltabpage

Drag and detach tabpages


I have several tabpages on a single tab control and I was wondering if anyone out there knows how I can program a way to drag tabpages off the tab control and into their own "form"....pulling the tabcontrol apart?

I'm using vb.net and did find many ways to move the order of tabpages on the tabcontrol but non om how to actually detach the tabpage and place \ drag it elsewhere on the screen?


Solution

  • Assuming WinForms, essentially you have to create a new Form and a new TabControl to house the TabPage you plan on moving.

    In its simplest form:

    Private Sub TabControl1_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControl1.MouseMove
      If (e.Button = MouseButtons.Left) Then
        TabControl1.DoDragDrop(TabControl1.SelectedTab, DragDropEffects.Move)
      End If
    End Sub
    
    Private Sub TabControl1_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControl1.GiveFeedback
      e.UseDefaultCursors = False
    End Sub
    
    Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
      If Control.MouseButtons <> MouseButtons.Left Then
        e.Action = DragAction.Cancel
        Dim f As New Form
        f.Size = New Size(400, 300)
        f.StartPosition = FormStartPosition.Manual
        f.Location = MousePosition
        Dim tc As New TabControl
        tc.Dock = DockStyle.Fill
        tc.TabPages.Add(TabControl1.SelectedTab)
        f.Controls.Add(tc)
        f.Show()
        Me.Cursor = Cursors.Default
      Else
        e.Action = DragAction.Continue
        Me.Cursor = Cursors.Help
      End If
    End Sub