C# Windows Forms ColoredTabPages Examples

Let us look at a simple C# Windows Forms ColoredTabPages example.

Example 1: ColoredTabPages

This example will comprise the following files:

  • ColoredTabPages.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). ColoredTabPages.cs

Create a file named ColoredTabPages.cs

Create our namespace and extend the Form class:

namespace Examples {
  class Form1 : Form {

Instantiate TabControl and TabPage as instance fields:

    private TabControl tabControl1 = new TabControl();
    private TabPage tabPageLightPink = new TabPage();
    private TabPage tabPageLightGreen = new TabPage();
    private TabPage tabPageLightBlue = new TabPage();
    private TabPage tabPageLightYellow = new TabPage();

Create our main method:

    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

Create our constructor:

    public Form1() {

Set the Form properties:

      ClientSize = new System.Drawing.Size(390, 270);
      Text = "Colored TabPages example";

Add our TabControl to our Form:

      Controls.Add(tabControl1);

Set the TabControl properties like Anchor, Location and Size:

      tabControl1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      tabControl1.Location = new System.Drawing.Point(10, 10);
      tabControl1.Size = new System.Drawing.Size(370, 250);

Add several TabPage objects to the TabControl via the AddRange() method:

      tabControl1.TabPages.AddRange(new TabPage[] { tabPageLightPink, tabPageLightGreen, tabPageLightBlue, tabPageLightYellow });

Let's give our TabPages different colors and Texts to differentiate them easily:

      tabPageLightPink.Text = "LightPink";
      tabPageLightPink.BackColor = System.Drawing.Color.LightPink;

      tabPageLightGreen.Text = "LightGreen";
      tabPageLightGreen.BackColor = System.Drawing.Color.LightGreen;

      tabPageLightBlue.Text = "LightBlue";
      tabPageLightBlue.BackColor = System.Drawing.Color.LightBlue;

      tabPageLightYellow.Text = "LightYellow";
      tabPageLightYellow.BackColor = System.Drawing.Color.LightYellow;

Here is the full code

using System;
using System.Windows.Forms;
namespace Examples {
  class Form1 : Form {
    private TabControl tabControl1 = new TabControl();
    private TabPage tabPageLightPink = new TabPage();
    private TabPage tabPageLightGreen = new TabPage();
    private TabPage tabPageLightBlue = new TabPage();
    private TabPage tabPageLightYellow = new TabPage();
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }
    public Form1() {
      ClientSize = new System.Drawing.Size(390, 270);
      Text = "Colored TabPages example";
      Controls.Add(tabControl1);
      tabControl1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      tabControl1.Location = new System.Drawing.Point(10, 10);
      tabControl1.Size = new System.Drawing.Size(370, 250);
      tabControl1.TabPages.AddRange(new TabPage[] { tabPageLightPink, tabPageLightGreen, tabPageLightBlue, tabPageLightYellow });
      tabControl1.Alignment = TabAlignment.Bottom;
      tabPageLightPink.Text = "LightPink";
      tabPageLightPink.BackColor = System.Drawing.Color.LightPink;

      tabPageLightGreen.Text = "LightGreen";
      tabPageLightGreen.BackColor = System.Drawing.Color.LightGreen;

      tabPageLightBlue.Text = "LightBlue";
      tabPageLightBlue.BackColor = System.Drawing.Color.LightBlue;

      tabPageLightYellow.Text = "LightYellow";
      tabPageLightYellow.BackColor = System.Drawing.Color.LightYellow;
    }

  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts