C# Programming > Windows Forms

C# Virtual Forms with TabControl

Virtual Forms

A create use of the .NET TabControl is to create "virtual" Forms within a WinForm. This allows applications to switch between "pages" of user controls. Using a TabControl makes it easy to switch between the virtual Forms during design-time.

There is not much code necessary to achieve the effect.

Removing TabControl Tabs

To make the virtual Forms not look like tab pages, we need to remove the tabs from a TabControl in .NET. The easiest way to do this is to create a custom control that inherits TabControl. Then we add the following code snippet to remove the tab headers during runtime:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x1328 && !DesignMode)
        m.Result = (IntPtr)1;
    else
        base.WndProc(ref m);
}

Using the Custom TabControl

All that is left is to use the our custom TabControl on our WinForms. Working with the control at design time is the same as any TabControl. The only difference is at runtime the user will not be able to switch between the pages. The only way to switch between them is programmatically. The SelectedIndex property indicates which page will be shown. The TabCount property can also be useful to make sure the SelectedIndex property is not set outside the bounds.

The sample source code at the bottom of the page shows a pretty simple example of how to use "virtual" Forms.

Back to C# Article List