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



借助下面的代码(由LarsTech - 拖动和分离标签页) 我能够分离标签页并将其放入新表单中。但是当我关闭该表单时,拖动的选项卡页面不会返回到其原始位置。如果有人可以帮助我编程,那就太好了!

 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

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

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 

更改此句子

f.TabIndex = TabControl1.SelectedTab.TabIndex

f.TabIndex = TabControl1.SelectedIndex

它工作正常。

TabIndex 表示

窗体中的顺序,但不表示 TabControl1 中的索引。

最新更新