TableLayoutPanel In C#

TableLayoutPanel control represents a panel that dynamically lays out its contents in a table format. You want to use a TableLayoutPanel in complex and sophisticated applications where you need to create dynamic layouts.

In this article, I will demonstrate how to create and use a TableLayoutPanel control in a Windows Forms application.

Creating a TableLayoutPanel

We can create a TableLayoutPanel control using the Forms designer at design-time or using the TableLayoutPanel class in code at run-time.

Design-time

To create a TableLayoutPanel control at design-time, you simply drag and drop a TableLayoutPanel control from Toolbox onto a Form in Visual Studio. After you drag and drop a TableLayoutPanel on a Form, the TableLayoutPanel looks like Figure 1. Once a TableLayoutPanel is on the Form, you can move it around and resize it using the mouse and set its properties and events.

TableLayoutPanel 
Figure 1

If you click on little handle of TableLayoutPanel, you will see options to add, remove, and edit rows and columns of the panel. Once rows and columns are added to a TableLayoutPanel, we can drag and drop child controls to these cells to manage the layouts.

Run-time

Creating a TableLayoutPanel control at run-time is merely a work of creating an instance of TableLayoutPanel class, setting its properties and adding TableLayoutPanel class to the Form controls.

The first step to create a dynamic TableLayoutPanel is to create an instance of TableLayoutPanel class. The following code snippet creates a TableLayoutPanel control object.

  1. TableLayoutPanel dynamicTableLayoutPanel = newTableLayoutPanel();  

In the next step, you may set properties of a TableLayoutPanel control. The following code snippet sets location, size and name properties of a TableLayoutPanel.

  1. dynamicTableLayoutPanel.Location = new System.Drawing.Point(26, 12);  
  2. dynamicTableLayoutPanel.Name = "TableLayoutPanel1";  
  3. dynamicTableLayoutPanel.Size = new System.Drawing.Size(228, 200);  
  4. dynamicTableLayoutPanel.TabIndex = 0;  

Once the TableLayoutPanel control is ready with its properties, the next step is to add the TableLayoutPanel to a Form. To do so, we use Form.Controls.Add method that adds TableLayoutPanel control to the Form controls and displays on the Form based on the location and size of the control. The following code snippet adds a TableLayoutPanel control to the current Form.

  1. Controls.Add(dynamicTableLayoutPanel);  

Adding Controls to a TableLayoutPanel

You can add controls to a TableLayoutPanel by dragging and dropping control to the TableLayoutPanel. We can add controls to a TableLayoutPanel at run-time by using its Add method.

ColumnStyle and RowStyle properties are used to specify number of columns and rows in a TableLayoutPanel. Add method is used to add a row and column to RowStyle and ColumnStyle respectively.

  1. dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 52 F));  
  2. dynamicTableLayoutPanel.ColumnStyles.Add(newColumnStyle(SizeType.Percent, 30 F));  

We can specify the position of a cell (row number and column number) in TableLayoutPanel.Controls.Add() method as following.

  1. dynamicTableLayoutPanel.Controls.Add(textBox1, 0, 0);  

The following code snippet creates a TableLayoutPanel, creates a TextBox and a CheckBox and adds these two controls to a TableLayoutPanel.

  1. privatevoid CreateButton_Click(object sender, EventArgs e) {  
  2.     tableLayoutPanel1.Visible = false;  
  3.     TableLayoutPanel dynamicTableLayoutPanel = newTableLayoutPanel();  
  4.     dynamicTableLayoutPanel.Location = new System.Drawing.Point(26, 12);  
  5.     dynamicTableLayoutPanel.Name = "TableLayoutPanel1";  
  6.     dynamicTableLayoutPanel.Size = new System.Drawing.Size(228, 200);  
  7.     dynamicTableLayoutPanel.BackColor = Color.LightBlue;  
  8.     // Add rows and columns  
  9.     dynamicTableLayoutPanel.ColumnCount = 3;  
  10.     dynamicTableLayoutPanel.RowCount = 5;  
  11.     dynamicTableLayoutPanel.ColumnStyles.Add(newColumnStyle(SizeType.Percent, 30 F));  
  12.     dynamicTableLayoutPanel.ColumnStyles.Add(newColumnStyle(SizeType.Percent, 30 F));  
  13.     dynamicTableLayoutPanel.ColumnStyles.Add(newColumnStyle(SizeType.Percent, 40 F));  
  14.     dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 52 F));  
  15.     dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 44 F));  
  16.     dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 44 F));  
  17.     dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 38 F));  
  18.     dynamicTableLayoutPanel.RowStyles.Add(newRowStyle(SizeType.Absolute, 8 F));  
  19.     TextBox textBox1 = newTextBox();  
  20.     textBox1.Location = newPoint(10, 10);  
  21.     textBox1.Text = "I am a TextBox5";  
  22.     textBox1.Size = newSize(200, 30);  
  23.     CheckBox checkBox1 = newCheckBox();  
  24.     checkBox1.Location = newPoint(10, 50);  
  25.     checkBox1.Text = "Check Me";  
  26.     checkBox1.Size = newSize(200, 30);  
  27.     // Add child controls to TableLayoutPanel and specify rows and column  
  28.     dynamicTableLayoutPanel.Controls.Add(textBox1, 0, 0);  
  29.     dynamicTableLayoutPanel.Controls.Add(checkBox1, 0, 1);  
  30.     Controls.Add(dynamicTableLayoutPanel);  
  31. }  

The output looks like Figure 2.

TableLayoutPanel Output
Figure 2

Show and Hide a TableLayoutPanel

I have seen in many applications when you want to show and hide a group of controls on a Form based on some condition. That is where a TableLayoutPanel is useful. Instead of showing and hiding individual controls, we can group controls that we want to show and hide and place them on two different TableLayoutPanels and show and hide the TableLayoutPanels. To show and hide a TableLayoutPanel, we use Visible property.

  1. dynamicTableLayoutPanel.Visible = false;  

Summary

In this article, we discussed how to use a TableLayoutPanel control in a Windows Forms application.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.