Category / Section
How to make closable tabpages in WinForms TabControlAdv?
2 mins read
Close the tabpage
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 a 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