Articles in this section
Category / Section

How to tear the TabPageAdv as a separate window by dragging it from the WinForms TabControlAdv?

2 mins read

Display the tab page as a separate window

TabControlAdv doesn’t have support to tear the TabPageAdv from the TabControlAdv and display it as a separate window. But this requirement can be achieved by handling the following TabControlAdv events.

1. MouseDown

2. MouseMove

3. MouseUp

C#

//Initializes the TabPageAdv
TabPageAdv SelectedPage = null;
bool isMouseDown = false;
bool isDragging = false;
Point mouseDownLocation = Point.Empty;
//The event raises when Mouse button is pressed.
private void tabControlAdv1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
        isMouseDown = true;
    else
    {
        isMouseDown = false;
        isDragging = false;
    }
    mouseDownLocation = e.Location;
}
//The event raises when Mouse is moved from the TabControlAdv
private void tabControlAdv1_MouseMove(object sender, MouseEventArgs e)
{
    if (isMouseDown && e.Button == System.Windows.Forms.MouseButtons.Left && mouseDownLocation.Y != e.Location.Y)
        isDragging = true;
    else
        isDragging = false;
}
//The event is raised when mouse pointer releases the TabControlAdv.
private void tabControlAdv1_MouseUp(object sender, MouseEventArgs e)
{
    //Initializes the newly created form
    Form2 form2 = new Form2();
    if (isDragging)
    {
        form2.Size = this.Size;
        if (this.tabControlAdv1.SelectedTab != null)
        {
            SelectedPage = this.tabControlAdv1.SelectedTab;
            form2.Text = SelectedPage.Text;
            form2.Show();
            //Adds the selected TabPage in Form2
            if (this.tabControlAdv1.TabPages.Count > 0)
                form2.tabControlAdv1.Controls.Add(SelectedPage);
        }
    }
    isMouseDown = false;
    isDragging = false;
}

VB

'Initializes the TabPageAdv
Private SelectedPage As TabPageAdv = Nothing
Private isMouseDown As Boolean = False
Private isDragging As Boolean = False
Private mouseDownLocation As Point = Point.Empty
'The event raises when the Mouse button is pressed.
Private Sub tabControlAdv1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles tabControlAdv1.MouseDown
    If e.Button = System.Windows.Forms.MouseButtons.Left Then
       isMouseDown = True
    Else
       isMouseDown = False
       isDragging = False
    End If
    mouseDownLocation = e.Location
End Sub
'The event is raised when Mouse moves from the TabControlAdv
Private Sub tabControlAdv1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles tabControlAdv1.MouseMove
    If isMouseDown AndAlso e.Button = System.Windows.Forms.MouseButtons.Left AndAlso mouseDownLocation.Y <> e.Location.Y Then
       isDragging = True
    Else
       isDragging = False
    End If
End Sub
'The event is raised when mouse pointer releases the TabControlAdv.
Private Sub tabControlAdv1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles tabControlAdv1.MouseUp
    'Initializes the newly created form
    Dim form2 As New Form2()
    If isDragging Then
       form2.Size = Me.Size
       If Me.tabControlAdv1.SelectedTab IsNot Nothing Then
          SelectedPage = Me.tabControlAdv1.SelectedTab
          form2.Text = SelectedPage.Text
          form2.Show()
          'Adds the selected TabPage in Form2
          If Me.tabControlAdv1.TabPages.Count > 0 Then
   form2.tabControlAdv1.Controls.Add(SelectedPage)
          End If
       End If
    End If
    isMouseDown = False
    isDragging = False
End Sub

Before separating the tab page from the TabControlAdv

Figure 1: Before separating the TabPageAdv from the TabControlAdv

After separating the tab page from the TabControlAdv

Figure 2: After separating the TabPageAdv from the TabControlAdv

Samples:

C#: Separate_TabControl_C#

VB: Separate_TabControl_VB

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied