VB.NET - 选项卡控件 - 拖动和分离选项卡

时间:2016-03-01 00:25:20

标签: vb.net winforms visual-studio-2013 tabs

借助以下代码(LarsTech - drag and detach tabpages 我能够分离一个标签页并将其放入一个新的表格中。但当我关闭该表格时,拖动的标签页不会返回其原始位置。 如果有人可以帮我编程,那就太棒了!

 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

1 个答案:

答案 0 :(得分:0)

我已经能够生成在表单关闭时将标签页返回到其原始位置的代码,但页面不会返回到原始索引位置。以下是更新后的代码:

Public f As 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
    f = New Form
    If Control.MouseButtons <> MouseButtons.Left Then
        e.Action = DragAction.Cancel
        AddHandler f.FormClosing, AddressOf ClosingDraggableWindow_EventHandler
        f.Size = New Size(400, 300)
        f.Name = TabControl1.SelectedTab.Text
        f.TabIndex = TabControl1.SelectedTab.TabIndex
        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()
    End If
End Sub

Sub ClosingDraggableWindow_EventHandler()
    Dim tbp As New TabPage()
    tbp.Text = f.Name
    Dim tbc As TabControl = f.Controls(0)
    Dim tbp2 As TabPage = tbc.TabPages(0)
    TabControl1.TabPages.Insert(f.TabIndex + 1, tbp2)
End Sub