【问题标题】:VB.NET - Tab Control - Drag and Detach TabVB.NET - 选项卡控件 - 拖放选项卡
【发布时间】:2016-03-01 00:25:19
【问题描述】:

借助下面的代码(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

【问题讨论】:

  • 您为什么希望它这样做?如果您希望发生这种情况,那么您必须编写代码来做到这一点。您已经拥有移动 TabPage 的代码,因此您可以对其进行调整,但您还必须记住它的来源,以便以后可以将其放回原处。
  • 对不起,我想我没有说清楚。我知道代码不应该将标签页返回到其原始位置。我无法生成可以完成该任务的代码,这就是我需要帮助的地方。
  • 正如我所说,您已经知道如何将TabPage 从一个地方移动到另一个地方,因为您已经在这样做了。您将有两种选择将其移回。首先,您可以让原始表单处理第二个表单的FormClosed 事件并将TabPage 拉回。其次,您可以让第二个表单在第一个表单上保留对 TabControl 的引用并将其推回。

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


【解决方案1】:

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

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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多