We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to make Closable TabPages

Platform: WinForms |
Control: TabControlAdv |
Published Date: April 30, 2011 |
Last Revised Date: July 10, 2014

 

Here we are going to make use of the DrawItem and MouseDown events.The DrawItem provides access to the Graphics object which helps us to draw an Close mark on Tabs.The GetTabRect method will give the Bounds of each tab.Finally we can check for the coresponding tab in the MouseDown event to close that Tab

C#

// DrawItem Event Handler

private void tabControlAdv1_DrawItem(object sender, Syncfusion.Windows.Forms.Tools.DrawTabEventArgs e)

{

   //Drawing the normal appearences

   e.DrawBackground();

   e.DrawBorders();

   e.DrawInterior();

   //Getting the bounds for Tab

   Rectangle tabrect = tabControlAdv1.GetTabRect(e.Index);

   tabrect.Offset(2, 2);

   tabrect.Width = 5;

   tabrect.Height = 5;

   //Initializing Brush and Pen

   Brush brush = new SolidBrush(Color.Black);

   Pen pen = new Pen(brush);

   //Drawing close mark

   e.Graphics.DrawLine(pen, tabrect.X, tabrect.Y, tabrect.X + tabrect.Width, tabrect.Y + tabrect.Height);

   e.Graphics.DrawLine(pen, tabrect.X + tabrect.Width, tabrect.Y, tabrect.X, tabrect.Y + tabrect.Height);

}

private void tabControlAdv1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){

   //Getting the current mouse location

   Point point = new Point(e.X,e.Y);

   //Checking all Tab bounds for a match

   for (int i = 0; i ; tabControlAdv1.TabCount; i++){

   Rectangle tabrect = tabControlAdv1.GetTabRect(i);

   tabrect.Offset(2, 2);

   tabrect.Width = 5;

   tabrect.Height = 5;

   if (tabrect.Contains(point))

   {

tabControlAdv1.TabPages.Remove(tabControlAdv1.TabPages[ i]);

   }

  }

 }

 

VB

'DrawItem Event Handler

Private Sub tabControlAdv1_DrawItem(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Tools.DrawTabEventArgs) Handles tabControlAdv1.DrawItem

   'Drawing the normal appearences

   e.DrawBackground()

   e.DrawBorders()

   e.DrawInterior()

   'Getting the bounds for Tab

   Dim tabrect As Rectangle = tabControlAdv1.GetTabRect(e.Index)

   tabrect.Offset(2, 2)

   tabrect.Width = 5

   tabrect.Height = 5

   'Initializing Brush and Pen

   Dim closeBrush As Brush = New SolidBrush(Color.Black)

   Dim closepen As Pen = New Pen(closeBrush)

   'Drawing close mark

    e.Graphics.DrawLine(closepen, tabrect.X, tabrect.Y, tabrect.X + tabrect.Width, tabrect.Y + tabrect.Height)

   e.Graphics.DrawLine(closepen, tabrect.X + tabrect.Width, tabrect.Y, tabrect.X, tabrect.Y + tabrect.Height)

End Sub

Private Sub tabControlAdv1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tabControlAdv1.MouseDown

   'Getting the current mouse location

   Dim mousepoint As Point = New Point(e.X, e.Y)

   Dim i As Integer

   'Checking all Tab bounds for a match

   For i = 0 To tabControlAdv1.TabCount

     Dim tabrect As Rectangle =    tabControlAdv1.GetTabRect(i)

   tabrect.Offset(2, 2)

   tabrect.Width = 5

   tabrect.Height = 5

   If tabrect.Contains(mousepoint) Then

tabControlAdv1.TabPages.Remove(tabControlAdv1.TabPages(i))

   Next i

End Sub

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon