7

Ex

  |Tab1|Tab2|Tab3| {    }
  |                     |
  |                     |
  |                     |
  |                     |
  |_____________________|

I am able to change the backcolor and forecolor of Tab.. but I want to change the color of that { } -- > Empty space is this possible to do that. .. It shows default winforms color..help me in dis..

 private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if ( e.Index == this.tabControl1.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName  = this.tabControl1.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle  recTab = e.Bounds;
        recTab = new Rectangle( recTab.X,  recTab.Y + 4,  recTab.Width,  recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);

    }
Aravind
  • 1,055
  • 2
  • 11
  • 15
  • I don't think the standard .NET TabControl allows you to set this "free space" to the right of the tabs to a specific background color - it's transparent and just shows the form's background color. If you **really** need this, you'll have to find another TabControl that supports this feature – marc_s Aug 06 '12 at 05:54
  • change the Appearance Property to "Normal" it's gonna change to transparent – S3ddi9 Aug 06 '12 at 05:59
  • There are more good reasons *not* to do this than there are to do it. – Cody Gray - on strike Aug 06 '12 at 07:46
  • @marc_s: The OP is setting his tab control DrawMode to OwnerDrawFixed to customize the style of the tab control. However, doing this no longer sets the background empty space to transparent and instead it sets it to system.control – Roast Sep 07 '12 at 18:47

3 Answers3

9

Try adding the following code to your DrawItem event handler. Don't forget to set the DrawMode to "OwnerdrawFixed".

You might have to tweak it a bit to cover some margins which aren't painted.

private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
      SolidBrush fillbrush= new SolidBrush(Color.Red);

  //draw rectangle behind the tabs
  Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
  Rectangle background = new Rectangle();
  background.Location = new Point(lasttabrect.Right, 0);

  //pad the rectangle to cover the 1 pixel line between the top of the tabpage and the start of the tabs
  background.Size = new Size(tabControl1.Right - background.Left, lasttabrect.Height+1);
  e.Graphics.FillRectangle(fillBrush, background);
}

'This answer is much better than prior one. But the tabCtrl is not defined. It has to be tabControl1 control.

John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
Roast
  • 1,715
  • 5
  • 24
  • 32
  • This bugs out when dragging under the start menu on windows 10. It will redraw the component with Control color – Hesein Burg Mar 24 '17 at 13:28
7

I think the only way to give that space a color is to override the OnPaintBackground method of the window, so just paste this on your form (window)

you must also change the Appearance Property to "Normal"

private void Form1_Load(object sender, EventArgs e)
{

}

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Rectangle lasttabrect = tabControl1.GetTabRect(tabControl1.TabPages.Count - 1);
    RectangleF emptyspacerect = new RectangleF(
            lasttabrect.X + lasttabrect.Width + tabControl1.Left,
            tabControl1.Top + lasttabrect.Y, 
            tabControl1.Width - (lasttabrect.X + lasttabrect.Width), 
            lasttabrect.Height);

    Brush b = Brushes.BlueViolet; // the color you want
    e.Graphics.FillRectangle(b, emptyspacerect );
}

for me it's working perfectly

enter image description here

Alex
  • 23,004
  • 4
  • 39
  • 73
S3ddi9
  • 2,121
  • 2
  • 20
  • 34
  • you don't have to you just write the code, or if you use visual studio write `protected override` and space it will gives you the Methods to override but "just write | paste the code" the method will be automatically overridden @user1484603 – S3ddi9 Aug 06 '12 at 07:19
  • Seddik i tried that also...but its not working for me.. as u said i override that method and pasted the code...but empty space is not getting painted... – Aravind Aug 06 '12 at 07:31
  • try to create a new project add only this TabControl & my code and see if it works, in my case I have a window with a TabControl, it works; Even if I write your code added the OnPaintBackground Method its working perfectly – S3ddi9 Aug 06 '12 at 07:41
  • Did you use `tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed` @user1484603 – S3ddi9 Aug 06 '12 at 07:51
  • Seddik its working just now checked with new project ..thanks a lot... :) – Aravind Aug 06 '12 at 08:24
  • Seddik is this same thing possible for DockWindows i mean can we fill the empty spaces.. – Aravind Aug 06 '12 at 09:26
  • give me the exact name of the control `DockWindows` – S3ddi9 Aug 06 '12 at 09:33
  • I am having a 3 r 4 Dockwindows and i am adding those windows in tab control.if i add like that and changed the OnPaintBackgroudChange() .. its not getting the color,,, – Aravind Aug 06 '12 at 09:40
  • Dockwindows where did you get this control from – S3ddi9 Aug 06 '12 at 09:52
  • sorry its Document not Dockwindows i explained u wrongly List m_lstFixedTab = new List Document doc = dockContainer1.Documents[iLoop] as Document; if (doc != null && !m_lstFixedTab.Contains(doc.Text)) { lstDocument.Add(doc); } this list is added as Docktabs and this Doctabs added in tabcontrol..how to paint rest the places – Aravind Aug 06 '12 at 09:55
  • @ Seddik http://stackoverflow.com/questions/11846982/how-to-set-backcolor-for-docking-windows-at-runtime-using-csharp pls see this – Aravind Aug 07 '12 at 13:22
3

you can also create a custom tabcontrol as you did

public class mytab : TabControl
{
    public mytab()
        : base()
    {
        this.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }

    private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
        Font fntTab;
        Brush bshBack;
        Brush bshFore;

        if (e.Index == this.SelectedIndex)
        {
            fntTab = new Font(e.Font, FontStyle.Bold);
            bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, SystemColors.Control, SystemColors.Control, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            bshFore = Brushes.Black;
            //bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(e.Bounds, Color.LightSkyBlue , Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
            //bshFore = Brushes.Blue;
        }
        else
        {
            fntTab = e.Font;
            bshBack = new SolidBrush(Color.Red);
            bshFore = new SolidBrush(Color.Aqua);

            //bshBack = new SolidBrush(Color.White);
            //bshFore = new SolidBrush(Color.Black);
        }

        string tabName = this.TabPages[e.Index].Text;
        StringFormat sftTab = new StringFormat();
        e.Graphics.FillRectangle(bshBack, e.Bounds);
        Rectangle recTab = e.Bounds;
        recTab = new Rectangle(recTab.X, recTab.Y + 4, recTab.Width, recTab.Height - 4);
        e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);


        Rectangle r = this.GetTabRect(this.TabPages.Count - 1);

        RectangleF tf =
            new RectangleF(r.X + r.Width,
            r.Y-5, this.Width - (r.X + r.Width)+5, r.Height+5);
        Brush b = Brushes.BlueViolet;

        e.Graphics.FillRectangle(b, tf);
    }

}
S3ddi9
  • 2,121
  • 2
  • 20
  • 34