C# Windows Forms Containers Examples

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

Example 1: Containers

This example will comprise the following files:

  • FlowLayoutPanel.cs
  • Form.cs
  • GroupBox.cs
  • Panel.cs
  • SplitContainer.cs
  • TabControl.cs
  • FlowLayoutPanel.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). FlowLayoutPanel.cs

Create a file named FlowLayoutPanel.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "FlowLayoutPanel example";

      flowLayoutPanelCenter.Parent = this;
      flowLayoutPanelCenter.Dock = DockStyle.Fill;
      flowLayoutPanelCenter.FlowDirection = FlowDirection.TopDown;
      flowLayoutPanelCenter.AutoScroll = true;

      panel1.Parent = flowLayoutPanelCenter;
      panel1.BackColor = System.Drawing.Color.LightPink;

      panel2.Parent = flowLayoutPanelCenter;
      panel2.BackColor = System.Drawing.Color.LightGreen;

      panel3.Parent = flowLayoutPanelCenter;
      panel3.BackColor = System.Drawing.Color.LightBlue;

      panel4.Parent = flowLayoutPanelCenter;
      panel4.BackColor = System.Drawing.Color.LightYellow;

      flowLayoutPanelTop.Parent = this;
      flowLayoutPanelTop.Dock = DockStyle.Top;
      flowLayoutPanelTop.FlowDirection = FlowDirection.LeftToRight;
      flowLayoutPanelTop.Height = 32;

      button1.Parent = flowLayoutPanelTop;
      button1.FlatStyle = FlatStyle.Popup;
      button1.Width = 24;
      button1.Text = "A";

      button2.Parent = flowLayoutPanelTop;
      button2.FlatStyle = FlatStyle.Popup;
      button2.Width = 24;
      button2.Text = "B";

      button3.Parent = flowLayoutPanelTop;
      button3.FlatStyle = FlatStyle.Popup;
      button3.Width = 24;
      button3.Text = "C";
    }

    private FlowLayoutPanel flowLayoutPanelTop = new FlowLayoutPanel();
    private Button button1 = new Button();
    private Button button2 = new Button();
    private Button button3 = new Button();

    private FlowLayoutPanel flowLayoutPanelCenter = new FlowLayoutPanel();
    private Panel panel1 = new Panel();
    private Panel panel2 = new Panel();
    private Panel panel3 = new Panel();
    private Panel panel4 = new Panel();
  }
}

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

*(a). Form.cs

Create a file named Form.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      buttonClose.Text = "Close";
      buttonClose.Location = new System.Drawing.Point(10, 10);
      buttonClose.Click += delegate (object sender, EventArgs e) {
        Close();
      };

      buttonExit.Text = "Exit";
      buttonExit.Location = new System.Drawing.Point(100, 10);
      buttonExit.Click += delegate (object sender, EventArgs e) {
        Application.ExitThread();
      };

      Text = "Form example";
      StartPosition = FormStartPosition.Manual;
      Location = new System.Drawing.Point(300, 200);
      Size = new System.Drawing.Size(640, 480);
      Controls.Add(buttonClose);
      Controls.Add(buttonExit);
      FormClosing += delegate(object sender, FormClosingEventArgs e) {
        e.Cancel = MessageBox.Show("Are you sure you want exit?", "Close Form", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No;
      };
    }

    private Button buttonClose = new Button();
    private Button buttonExit = new Button();
  }
}

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

*(a). GroupBox.cs

Create a file named GroupBox.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      groupBox1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
      groupBox1.Text = "GroupBox 1";
      groupBox1.Bounds = new System.Drawing.Rectangle(10, 10, 305, 460);

      groupBox2.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      groupBox2.Bounds = new System.Drawing.Rectangle(325, 10, 305, 460);

      ClientSize = new System.Drawing.Size(640, 480);
      Text = "GroupBox example";
      Controls.AddRange(new Control[] { groupBox1, groupBox2 });
    }

    private GroupBox groupBox1 = new GroupBox();
    private GroupBox groupBox2 = new GroupBox();
  }
}

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

*(a). Panel.cs

Create a file named Panel.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      panel1.BorderStyle = BorderStyle.FixedSingle;
      panel1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
      panel1.Bounds = new System.Drawing.Rectangle(10, 10, 305, 460);

      panel2.BorderStyle = BorderStyle.Fixed3D;
      panel2.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      panel2.Bounds = new System.Drawing.Rectangle(325, 10, 305, 460);

      StartPosition = FormStartPosition.Manual;
      Location = new System.Drawing.Point(200, 200);
      ClientSize = new System.Drawing.Size(640, 480);
      Text = "Panel example";
      Controls.AddRange(new Control[] { panel1, panel2});
    }

    private Panel panel1 = new Panel();
    private Panel panel2 = new Panel();
  }
}

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

*(a). SplitContainer.cs

Create a file named SplitContainer.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "SplitContainer example";
      ClientSize = new System.Drawing.Size(300, 300);

      splitContainerLeft.Parent = this;
      splitContainerLeft.Dock = DockStyle.Fill;
      splitContainerLeft.SplitterDistance = 70;
      splitContainerLeft.Panel1.BackColor = System.Drawing.Color.LightBlue;

      splitContainerRight.Parent = splitContainerLeft.Panel2;
      splitContainerRight.Dock = DockStyle.Fill;
      splitContainerRight.SplitterDistance = 160;
      splitContainerRight.Panel2.BackColor = System.Drawing.Color.LightYellow;

      splitContainerCenter.Parent = splitContainerRight.Panel1;
      splitContainerCenter.Dock = DockStyle.Fill;
      splitContainerCenter.Orientation = Orientation.Horizontal;
      splitContainerCenter.SplitterDistance = 147;
      splitContainerCenter.Panel1.BackColor = System.Drawing.Color.LightPink;
      splitContainerCenter.Panel2.BackColor = System.Drawing.Color.LightGreen;
    }

    private SplitContainer splitContainerLeft = new SplitContainer();
    private SplitContainer splitContainerRight = new SplitContainer();
    private SplitContainer splitContainerCenter = new SplitContainer();
  }
}

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

*(a). TabControl.cs

Create a file named TabControl.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      tabPage3.Text = "tabPage3";

      tabControl1.Location = new System.Drawing.Point(10, 10);
      tabControl1.Size = new System.Drawing.Size(370, 250);

      tabControl1.TabPages.Add("tabPage1");
      tabControl1.TabPages.Add("tabPageé", "tabPage2");
      tabControl1.TabPages.Add(tabPage3);

      tabControl1.TabPages[0].UseVisualStyleBackColor = true;
      tabControl1.TabPages[1].BackColor = System.Drawing.SystemColors.Window;

      ClientSize = new System.Drawing.Size(390, 270);
      Text = "TabControl example";
      Controls.Add(tabControl1);
    }

    private TabControl tabControl1 = new TabControl();
    private TabPage tabPage3 = new TabPage();
  }
}

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

*(a). FlowLayoutPanel.cs

Create a file named FlowLayoutPanel.cs

Here is the full code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      Text = "TableLayoutPanel example";

      tableLayoutPanel1.Parent = this;
      tableLayoutPanel1.Dock = DockStyle.Fill;
      tableLayoutPanel1.ColumnCount = columnCount;
      tableLayoutPanel1.RowCount = rowCount;
      for (int index = 0; index < tableLayoutPanel1.ColumnCount; index++)
        tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, tableLayoutPanel1.ColumnCount));
      for (int index = 0; index < tableLayoutPanel1.RowCount; index++)
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, tableLayoutPanel1.RowCount));

      for (int y = 0; y < buttons.GetLength(1); y++) {
        for (int x = 0; x < buttons.GetLength(0); x++) {
          buttons = new CheckBox();
          buttons.AutoCheck = false;
          buttons.Appearance = Appearance.Button;
          buttons.BackColor = Color.LightGray;
          buttons.Parent = tableLayoutPanel1;
          buttons.Margin = new Padding(0);
          buttons.Size = new Size(1000, 1000);
          buttons.FlatStyle = FlatStyle.Flat;
          buttons.Tag = Tuple.Create(x, y);
          buttons.TextAlign = ContentAlignment.MiddleCenter;
          buttons.MouseDown += delegate (object sender, MouseEventArgs e) {
            if (e.Button == MouseButtons.Right) {
              CheckBox checkBox = sender as CheckBox;
              int currentX = (checkBox.Tag as Tuple<int, int>).Item1;
              int currentY = (checkBox.Tag as Tuple<int, int>).Item2;
              if (!buttons[currentX, currentY].Checked) {
                if (buttons[currentX, currentY].Text == "") buttons[currentX, currentY].Text = "F";
                else if (buttons[currentX, currentY].Text == "F") buttons[currentX, currentY].Text = "?";
                else if (buttons[currentX, currentY].Text == "?") buttons[currentX, currentY].Text = "";
              }
            }
          };

          buttons.Click += delegate(object sender, EventArgs e) {
            CheckBox checkBox = sender as CheckBox;
            int currentX = (checkBox.Tag as Tuple<int, int>).Item1;
            int currentY = (checkBox.Tag as Tuple<int, int>).Item2;
            if (!checkBox.Checked && checkBox.Text == "") {

              if (mines[currentX, currentY]) {
                buttons[currentX, currentY].BackColor = Color.Red;
                for (int index1 = 0; index1 < buttons.GetLength(1); index1++) {
                  for (int index2 = 0; index2 < buttons.GetLength(0); index2++) {
                    if (mines[index2, index1]) {
                      buttons[index2, index1].Checked = true;
                      buttons[index2, index1].Text = "M";
                    }
                  }
                }
                MessageBox.Show("Booom", "Mine");
                InitGames();
              } else {
                checknNeighbors(currentX, currentY);
              }
            }
          };
        }
      }
      InitGames();
    }

    void checknNeighbors(int currentX, int currentY) {
      int mineCount = CheckedCell(currentX, currentY);
      if (mineCount == 0) {
        for (int index1 = currentY - 1; index1 <= currentY + 1; index1++) {
          for (int index2 = currentX - 1; index2 <= currentX + 1; index2++) {
            if (index1 >= 0 && index1 < columnCount && index2 >= 0 && index2 < rowCount && buttons[index2, index1].Checked == false && CheckedCell(index2, index1) == 0) {
              checknNeighbors(index2, index1);
            }
          }
        }
      }
    }

    int CheckedCell(int currentX, int currentY) {
      buttons[currentX, currentY].Checked = true;
      buttons[currentX, currentY].BackColor = Color.White;

      int mineCount = 0;
      for (int index1 = currentY - 1; index1 <= currentY + 1; index1++) {
        for (int index2 = currentX - 1; index2 <= currentX + 1; index2++) {
          if (index1 >= 0 && index1 < columnCount && index2 >= 0 && index2 < rowCount && mines[index2, index1]) {
            mineCount++;
          }
        }
      }

      if (mineCount == 6) buttons[currentX, currentY].ForeColor = Color.Black;
      if (mineCount == 5) buttons[currentX, currentY].ForeColor = Color.DarkRed;
      if (mineCount == 4) buttons[currentX, currentY].ForeColor = Color.DarkBlue;
      if (mineCount == 3) buttons[currentX, currentY].ForeColor = Color.Red;
      if (mineCount == 2) buttons[currentX, currentY].ForeColor = Color.Green;
      if (mineCount == 1) buttons[currentX, currentY].ForeColor = Color.Blue;
      if (mineCount == 0) buttons[currentX, currentY].ForeColor = Color.Black;
      if (mineCount != 0) buttons[currentX, currentY].Text = mineCount.ToString();
      return mineCount;
    }

    void InitGames() {
      for (int y = 0; y < buttons.GetLength(1); y++) {
        for (int x = 0; x < buttons.GetLength(0); x++) {
          mines = false;
          buttons.BackColor = Color.LightGray;
          buttons.ForeColor = Color.Black;
          buttons.Checked = false;
          buttons.Text = "";
        }
      }

      Random rand = new Random();
      for (int count = 0; count < mineCount; count++) {
        int x = rand.Next(0, columnCount);
        int y = rand.Next(0, rowCount);

        if (mines == true) {
          count--;
          continue;
        }
        mines = true;
      }
    }

    readonly static int mineCount = 10;
    readonly static int columnCount = 9;
    readonly static int rowCount = 9;
    private TableLayoutPanel tableLayoutPanel1 = new TableLayoutPanel();
    CheckBox[,] buttons = new CheckBox[rowCount, columnCount];
    bool[,] mines = new bool[columnCount, rowCount];
  }
}

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