拖动和分离tabpages

时间:2014-02-26 13:47:25

标签: vb.net tabcontrol tabpage

我在一个标签控件上有几个标签页面,我想知道是否有人知道如何编写一种方法将标签页从标签控件拖到自己的“表单”中....将tabcontrol分开?

我正在使用vb.net并且确实找到了很多方法来移动tabcontrol上的tabpages顺序,但是不知道如何实际分离tabpage并将其拖放到屏幕上的其他位置?

2 个答案:

答案 0 :(得分:2)

假设WinForms,基本上你必须创建一个新的Form和一个新的TabControl来容纳你计划移动的TabPage。

最简单的形式:

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 :(得分:1)

在此,我想根据LarsTech的代码提交我的个人改进。此版本允许选项卡在表单关闭时返回其原始父级,并要求用户在触发拖动事件之前将选项卡拖动一个已定义的像素偏移量。

Dim pTabControlClickStartPosition As Point
Dim iTabControlFlyOffPixelOffset As Integer = 20

Private Sub TabControlMain_MouseDown(sender As Object, e As MouseEventArgs) Handles TabControlMain.MouseDown
    If (e.Button = MouseButtons.Left) Then
        pTabControlClickStartPosition = e.Location
    End If
End Sub

Private Sub TabControlMain_MouseMove(sender As Object, e As MouseEventArgs) Handles TabControlMain.MouseMove
    If (e.Button = MouseButtons.Left) Then
        Dim iMouseOffset_X = pTabControlClickStartPosition.X - e.X
        Dim iMouseOffset_Y = pTabControlClickStartPosition.Y - e.Y

        If iMouseOffset_X > iTabControlFlyOffPixelOffset Or iMouseOffset_Y > iTabControlFlyOffPixelOffset Then
            TabControlMain.DoDragDrop(TabControlMain.SelectedTab, DragDropEffects.Move)
            pTabControlClickStartPosition = New Point
        End If
    Else
        pTabControlClickStartPosition = New Point
    End If
End Sub

Private Sub TabControlMain_GiveFeedback(sender As Object, e As GiveFeedbackEventArgs) Handles TabControlMain.GiveFeedback
    e.UseDefaultCursors = False
End Sub

Private Sub TabControlMain_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControlMain.QueryContinueDrag
    If Control.MouseButtons <> MouseButtons.Left Then
        e.Action = DragAction.Cancel
        Dim f As New Form
        f.Size = New Size(800, 400)
        f.StartPosition = FormStartPosition.Manual
        f.Location = MousePosition
        Dim tc As New TabControl
        tc.Dock = DockStyle.Fill
        tc.TabPages.Add(TabControlMain.SelectedTab)
        f.Controls.Add(tc)
        AddHandler f.FormClosing, Sub(sender1 As Object, e1 As EventArgs)
                                      TabControlMain.TabPages.Add(tc.SelectedTab)
                                  End Sub
        f.Show()
        Me.Cursor = Cursors.Default
    Else
        e.Action = DragAction.Continue
        Me.Cursor = Cursors.SizeAll
    End If
End Sub

<强>更新 正如评论中所指出的那样,当子窗体关闭时,TabPage将被放回原始的TabControl中,但不会放在先前的顺序中。

为避免此行为,我们可以使用Tag属性为TabPages指定优先级。

假设我们的TabControl中有三个TabPages,顺序为:&#34; Tab C&#34;,&#34; Tab A&#34;,Tab B&#34;。现在我们必须设置TabA.Tag = 2,TabB.Tag = 3,TabC.Tag = 1.

然后将f.FormClosing代码更改为:

'Get the first TabPage with a greater Tag value than the one being inserted.
Dim followingTab = TabControlMain.TabPages.Cast(Of TabPage)().FirstOrDefault(Function(tp) CInt(tp.Tag) > CInt(tc.SelectedTab.Tag))

If followingTab Is Nothing Then
    'There is no following tab so add this one to the end.
    TabControlMain.TabPages.Add(tc.SelectedTab)
Else
    'Insert this tab before the following one.
TabControlMain.TabPages.Insert(TabControlMain.TabPages.IndexOf(followingTab), tc.SelectedTab)
    End If

来源:http://www.vbforums.com/showthread.php?766367-Tabpage-Add-or-insert-tabpage-with-order-determined&s=1b895ca4d79ab29e7c00c4b07a4ce142&p=4692139&viewfull=1#post4692139