C# Exercises

Home AgriMetSoft About Contact

How to Use Tabcontrol in | C# and Coloring Selected Tab

	    	
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tab_Control_in_Winform_Application
{
  public partial class Form1 : Form
  {
    // c# tabcontrol color selected tab
    public Form1()
    {
      InitializeComponent();
    }

 
   private void Form1_Load(object sender, EventArgs e)
    {
      tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
    }

    private void tabControl1_DrawItem(object sender, 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, Color.LightSkyBlue, Color.LightGreen, System.Drawing.Drawing2D.LinearGradientMode.BackwardDiagonal);
        bshFore = Brushes.Blue;
      }
      else
      {
        fntTab = e.Font;
        bshBack = new SolidBrush(Color.White);
        bshFore = new SolidBrush(Color.Black);
      }
      string tabName = this.tabControl1.TabPages[e.Index].Text;
      StringFormat sftTab = new StringFormat(StringFormatFlags.NoClip);
      sftTab.Alignment = StringAlignment.Center;
      sftTab.LineAlignment = StringAlignment.Center;
      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);

    }
  }
}

		
	 


Download the project of Visual Studio 2013 in DropBox Download


C# Winforms | How to Add Tabcontrol | Color Selected Tab


List of Exercises