Click here to Skip to main content
11,708,116 members (73,715 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 WinForm
I created one extended tab control. In that tab control I want to put close button(as like chrome browser tab). How it will happen? Please give any suggestion. This following code I used for creating tab control

public class ExtendedTabControl : TabControl
    {
        public ExtendedTabControl()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw |
                ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
 
            this.Invalidate();
            BorderWidth =1;
 
        }
        public Color ActiveBorderColor { get; set; }
        public Color InactiveBorderColor { get; set; }
        public Color ActiveTabColor { get; set; }
        public Color InActiveTabColor { get; set; }
        public float BorderWidth { get; set; }
 

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            //TabPages[this.SelectedIndex]

            this.Invalidate();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            for (int i = 0; i < this.TabCount; i++)
            {
                                if (i != this.SelectedIndex)
                {
                    DrawTab(e.Graphics, i, false);
                }
            }
            //TabPages[this.SelectedIndex].BackColor = ActiveTabColor;
            DrawTab(e.Graphics, this.SelectedIndex, true);
 
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);

 

 

 

        }
        private void DrawTab(Graphics g, int i, bool isSelected)
        {
            if (i < 0)
            {
                return;
            }
            Rectangle rectangle = GetTabRect(i);
            if (isSelected)
            {
                rectangle.Height += 2;
            }
 
            GraphicsPath path = new GraphicsPath();
            Point p1 = new Point(rectangle.X + 20, rectangle.Y);
            Point p2 = new Point(rectangle.X, rectangle.Y + rectangle.Size.Height);
            path.AddLine(p1, p2);
            Point p3 = new Point(rectangle.X + rectangle.Width, rectangle.Y);
            path.AddLine(p1, p3);
            Point p4 = new Point(rectangle.Left + rectangle.Width + 24, rectangle.Top + rectangle.Height);
            path.AddLine(p3, p4);
            path.AddLine(p4, p2);
            if (isSelected)
            {
 
                using (Brush brush = new SolidBrush(ActiveTabColor))
                {
                    g.FillPath(brush, path);
                }
 
                using (Pen pen = new Pen(ActiveBorderColor, BorderWidth))
                {
                    DrawTabBorder(g, pen);
                }
 

            }
            else
            {  
                using (Brush brushI = new SolidBrush(InActiveTabColor))
                {
                    g.FillPath(brushI, path); 
                }
                using (Pen pen = new Pen(InactiveBorderColor, BorderWidth))
                {
                    DrawTabBorder(g, pen);
                }
 
            }
 
        }
 
        private void DrawTabBorder(Graphics g, Pen pen)
        {
            if (this.SelectedIndex < 0)
            {
                return;
            }
            Rectangle selectedRectangel = GetTabRect(this.SelectedIndex);
            #region coment
            //selectedRectangel.Width -= 6;
            //selectedRectangel.X += 1;        
            //Point pRight = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
                                     //this.SelectedTab.Bounds.Location.Y - 1);

            //Point pRightBottam = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
                                           //this.SelectedTab.Bounds.Location.Y + this.SelectedTab.Bounds.Height + 1);
            //Point pLeftTabStart = new Point(selectedRectangel.X, pLeft.Y);
            //Point pLeftTabStop = new Point(selectedRectangel.X + selectedRectangel.Width + selectedRectangel.Height + 1 ,
#endregion

            Point pLeft = new Point(this.SelectedTab.Bounds.Location.X, this.SelectedTab.Bounds.Location.Y);
 
            Point pLeftBottam = new Point(this.SelectedTab.Bounds.Location.X - 1, 
                                          this.SelectedTab.Bounds.Location.Y + this.SelectedTab.Bounds.Height + 1);
            Point pt1 = selectedRectangel.Location;
            pt1.X += 20;
            Point pt2 = new Point(selectedRectangel.Location.X + selectedRectangel.Width + 1, 
                                   selectedRectangel.Location.Y + 1);
            Point pt3 = new Point(selectedRectangel.X, selectedRectangel.Y + selectedRectangel.Height);
            Point pt4 = new Point(selectedRectangel.Left+selectedRectangel.Width+24,
                                   selectedRectangel.Top+selectedRectangel.Height);
            Point pt6 = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
                                  this.SelectedTab.Bounds.Location.Y - 1);
            Point pt7 = new Point(pLeftBottam.X+this.SelectedTab.Bounds.Width,pLeftBottam.Y);
            Point pt8 = pLeftBottam;
 
            GraphicsPath pathTab = new GraphicsPath();
            pathTab.AddLine(pt3, pt1);
            pathTab.AddLine(pt1, pt2);
            pathTab.AddLine(pt2, pt4);
            pathTab.AddLine(pt4, pt6);
            pathTab.AddLine(pt6, pt7);
            pathTab.AddLine(pt7, pt8);
            pathTab.AddLine(pt8, pLeft);
            pathTab.AddLine(pLeft, pt3);
 
            g.SmoothingMode = SmoothingMode.AntiAlias;
            pathTab.Widen(pen);
            g.DrawPath(pen, pathTab);
 

 
        }
        protected override void OnSelected(TabControlEventArgs e)
        {
            TabPage page = this.SelectedTab;
            page.BackColor = ActiveTabColor;
        }
}
Posted 27-Mar-13 3:24am
Edited 27-Mar-13 4:09am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please, read this: Putting a Close button on top right of TabControl[^]. There you'll find many useful tips.

CP Knowledge Base articles:
A TabControl with tab page closing capability[^]
FireFox-like Tab Control[^]
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 27-Mar-13 9:58am
   
5ed.
—SA
Maciej Los at 27-Mar-13 9:59am
   
Thank you, Sergey ;)
Your suggestions are very useful!
Sergey Alexandrovich Kryukov at 27-Mar-13 10:07am
   
Thank you, Maciej.
—SA
Neelendu Kumar at 27-Mar-13 10:11am
   
actually i want image as like close sign image on the tab header.and when i click that close image the tab should be removed from that tab control.is it possible? please give me some suggestion how to do it?
Maciej Los at 27-Mar-13 15:47pm
   
Please, follow the links. There you'll find an example code.
Neelendu Kumar at 28-Mar-13 2:35am
   
i added ondraw item method and done all drawing work but the ondraw item method is not working..what to do
Maciej Los at 28-Mar-13 2:47am
   
How can i help you, if i don't see your code. Please, post new question and share your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 230
1 Mika Wendelius 180
2 OriginalGriff 168
3 Sergey Alexandrovich Kryukov 145
4 Peter Leow 140
0 OriginalGriff 9,198
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,921
4 Mika Wendelius 3,856


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 27 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100