How to Add Tabs to a C# Windows Application

Organizing Form Components into Tabs with C#

Adding C# Tabs to a Windows Application - Mark Alexander Bain
Adding C# Tabs to a Windows Application - Mark Alexander Bain
A programmer can make Windows forms simpler to use for their users by adding tabs, and this can be easily achieved by using some C# code.

A form used in a Windows application can quickly become very complicated. It will probably contain many components such as:

  • Labels
  • Text boxes
  • ComboBoxes
  • Buttons

One way to organize the application is to use multiple forms, but this in itself can add further complexity. Another neater way of simplifying the application is to group the components into tabs. The C# programmer can, therefore, create a single form containing a number of clearly labeled tabs, each of which will contain components grouped according to their intended purpose.

Creating a Tabbed Form with C#

The C# programmer always creates a Windows form in the same way:

  • include any required classes (with the "using" key word)
  • extend the default Form class

For example:

using System;
using System.Windows.Forms;
using System.Drawing;
public class MainForm : Form {

The form then needs a tab control:

private TabControl tabMain = new TabControl();

And the tab control will contain a number of tab pages:

private TabPage tabOptions = new TabPage("Options");
private TabPage tabGeneral = new TabPage("General");
private TabPage tabReportMain = new TabPage ("Reports");

The code will need to define any components to be used in the tabs:

private Label lblReportNo = new Label ();
private TextBox txtReportNo = new TextBox ();
private Button btnRunReports = new Button ();

And the class initialization will add the tab control to the form, the tab pages to the tab control, and the form components to the tab pages:

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

So, first the code adds the tab control to the form:

this.Controls.Add(tabMain);

Then it adds tab pages to the tab control::

tabMain.Controls.Add(tabOptions);
tabMain.Controls.Add(tabGeneral);
tabMain.Controls.Add(tabReportMain);

Finally the code adds any controls to the appropriate tab page:

tabGeneral.Controls.Add(btnRunReports);
btnRunReports.AutoSize = true;
btnRunReports.Text = "Create Reports";
tabOptions.Controls.Add(lblReportNo);
lblReportNo.AutoSize = true;
lblReportNo.Text = "Number of Reports";
lblReportNo.Location = new Point (0,0);
tabOptions.Controls.Add(txtReportNo);
txtReportNo.Text = "1";
txtReportNo.Location = new Point (lblReportNo.Width, 0);

If the programmer now compiles the form then they will see a form with three tabs, each of which will themselves contain a number of form components.

Resizing Tabs with C#

When a user views the new form one thing that will be immediately obvious is that the default tabs are quite small, and will remain the same size even if the size of the form is changed. This can, of course be remedied with a C# function:

void setTabSize () {
tabMain.Width = this.Width;
tabMain.Height = this.Height;
}

If this function is called as part of the class initialization then the tabs will fit the form. However, if the user manually changes the size of the form then size of the tabs will remain unchanged. An Event Handler is required for the size of the tab to update when the size of the form changes:

this.SizeChanged += new EventHandler(MainForm_SizeChanged);

Now the event handler will run a new function whenever the user changes the size of the form:

void MainForm_SizeChanged(object sender, EventArgs e) {
setTabSize ();
}

And size of the tabs will always match the size of the form.

Summary

A C# programmer can use tabs in a Windows form to organize the form's components. To do this they must:

  • extend the default form class
  • add a tab control to their form
  • add tab pages to the tab control
  • add any components to the tab pages

The end result is a single, well-organized form suitable for any Windows application.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement

Comments

Oct 12, 2009 3:03 AM
Guest :
What a beautiful, simple article. It takes true genius to write like this.
Apr 21, 2010 1:04 PM
Guest :
beautiful article thanks..
2 Comments
Advertisement
Advertisement