Change the Tab size of tabControl

Lorenzo Belfanti picture Lorenzo Belfanti · Feb 2, 2016 · Viewed 23k times · Source

I redraw the graphics of the Tab for the TabControl but I can't set the Width of it.

What I want is that the text of the Tab and the icon is contained in its size.

Now is something like this:

tabs are overlaid

My Code

private void tabControlForm_DrawItem(object sender, DrawItemEventArgs e)
{

   try
   {
       using (Brush br = new SolidBrush(TabColors[tabControlForm.TabPages[e.Index]]))
       {
           Rectangle rect = e.Bounds;

           rect.Width += 10;

           e.Graphics.FillRectangle(br, rect);
           SizeF sz = e.Graphics.MeasureString(tabControlForm.TabPages[e.Index].Text, e.Font);
           e.Graphics.DrawString(tabControlForm.TabPages[e.Index].Text, e.Font, Brushes.Black, rect.Left + (rect.Width - sz.Width) / 2, rect.Top + (rect.Height - sz.Height) / 2 + 1);

           using (var src = new Bitmap(System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream("Castor.Gestionale.images.close-button.png")))
           {
              e.Graphics.DrawImage(src, rect.Right - 18, rect.Top + 10);
           }

           e.Graphics.DrawRectangle(Pens.DarkGray, rect);
           e.DrawFocusRectangle();
       }
   }
   catch {}
}

Thanks

Answer

TaW picture TaW · Feb 2, 2016

Actually you can set the size of the tabs, but not individually.

The combination of SizeMode = Fixed and some suitable value for the TabControl.Itemsize will create any size, but always the same..:

enter image description here

So for individually enlarging each tab to make the icon fit you indeed need to use Ian's 'spacey' method..