Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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
Updated 27-Mar-13 4:09am
v2

1 solution

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[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 27-Mar-13 9:58am    
5ed.
—SA
Maciej Los 27-Mar-13 9:59am    
Thank you, Sergey ;)
Your suggestions are very useful!
Sergey Alexandrovich Kryukov 27-Mar-13 10:07am    
Thank you, Maciej.
—SA
Neelendu Kumar 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 27-Mar-13 15:47pm    
Please, follow the links. There you'll find an example code.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900