Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Windows Controls C# » TableLayoutPanel in C#

TableLayoutPanel in C#

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

Author Rank :
Page Views : 13027
Downloads : 179
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
TableLayoutPanelSampleCSharp.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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 mouse and set its properties and events.

TableLayoutPanelImg1.jpg 

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, set its properties and adds TableLayoutPanel class to the Form controls.

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

TableLayoutPanel dynamicTableLayoutPanel = new TableLayoutPanel();

 

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.

dynamicTableLayoutPanel.Location = new System.Drawing.Point(26, 12);

dynamicTableLayoutPanel.Name = "TableLayoutPanel1";

dynamicTableLayoutPanel.Size = new System.Drawing.Size(228, 200);

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.

 

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.

dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 52F));

dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));

 

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

 

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.

private void CreateButton_Click(object sender, EventArgs e)

{

    tableLayoutPanel1.Visible = false;

 

    TableLayoutPanel dynamicTableLayoutPanel = new TableLayoutPanel();

    dynamicTableLayoutPanel.Location = new System.Drawing.Point(26, 12);

    dynamicTableLayoutPanel.Name = "TableLayoutPanel1";

    dynamicTableLayoutPanel.Size = new System.Drawing.Size(228, 200);

    dynamicTableLayoutPanel.BackColor = Color.LightBlue;

 

    // Add rows and columns

    dynamicTableLayoutPanel.ColumnCount = 3;

    dynamicTableLayoutPanel.RowCount = 5;

 

    dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));

    dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));

    dynamicTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F));          

    dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 52F));

    dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44F));

    dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 44F));

    dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 38F));

    dynamicTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 8F));

  

    TextBox textBox1 = new TextBox();

    textBox1.Location = new Point(10, 10);

    textBox1.Text = "I am a TextBox5";

    textBox1.Size = new Size(200, 30);

 

    CheckBox checkBox1 = new CheckBox();

    checkBox1.Location = new Point(10, 50);

    checkBox1.Text = "Check Me";

    checkBox1.Size = new Size(200, 30);

 

    // Add child controls to TableLayoutPanel and specify rows and column

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

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

 

    Controls.Add(dynamicTableLayoutPanel);

}

 

The output looks like Figure 2.

TableLayoutPanelImg2.jpg
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 show and hide 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.

dynamicTableLayoutPanel.Visible = false;

 

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

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
take input from a table layout panel by sumit On October 18, 2010
i want to use this control to design a cash memo. but i cant write in the boxes or grids of table layout panel. can you suggest me how can i take input to my database through this control. or any suggestion for designing a cash memo. Thanks in advance.
Reply | Email | Modify 
Re: take input from a table layout panel by Mahesh On November 7, 2010
For a cash memo, you probably have to use bunch of panels and controls. What fields do you have on the window?
Reply | Email | Modify 
Re: take input from a table layout panel by Mahesh On November 7, 2010
Can you attach an image of your design?
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.