Why another TabControl?

The .NET Framework contains a wide range of user interface controls, but they
offer only a basic look and feel. Today's user expects a richer experience that
you just cannot build with the out-of-the-box controls. The Magic Library (of
which this TabControl is a major part) aims to solve this by providing a set of
user interface elements that add the sophistication expected from modern
Windows applications.

Downloads

The first download TabControl Sample contains a Form with a
TabControl instance that allows the various properties of the TabControl to be
experimented with. The source code for the actual control is inside the
separate second download. At nearly 1MB in size, I thought people would prefer
to download the sample before deciding if they want to install the entire
source code project!

Class Crownwood.Magic.TabControl

The TabControl is used to manage a collection of TabPage instances
which themselves are used to describe an individual page. Only a single page
can be displayed at any time and a row of tabs are shown to allow the user to
select between the available pages.

Creating tab pages in Visual Studio .NET

You can use the designer to modify a TabControl instance along with its TabPage
instances. First you will need to add the control to the toolbox by
right-clicking the toolbox window and selecting the customize option. Navigate
to the appropriate directory and select the Magic Library DLL. The toolbox will
now list the extra controls that are exposed by the library including the TabControl.
Drag and drop a new instance onto your Form and select the TabPages
property.

This will cause a dialog to appear that allows the creation and removal of
pages to the control instance. It also allows you to modify the properties for
each page added. Dismiss this dialog and you can now click on the control tabs
in order to select the page you want to configure. As the TabPage class
is derived from the Panel class you can now drag and drop other controls
onto the page as desired. The designer will automatically generate the code
needed to cause this configuration to be persisted as code in your Form.

The TabPages Collection

The TabControl exposes a property called TabPages that allows the
developer to add, remove and modify pages displayed by the control. Use the
standard methods to modify this collection as you would with any of the
framework collections.

Here is an example that shows the collection being manipulated: -

Crownwood.Magic.Controls.TabControl tab =
new Crownwood.Magic.Controls.TabControl();
Crownwood.Magic.Controls.TabPage tabPage1 =
new Crownwood.Magic.Controls.TabPage();
Crownwood.Magic.Controls.TabPage tabPage2 =
new Crownwood.Magic.Controls.TabPage();
// Add a range of pages to the collection
tab.TabPages.AddRange(new TabPage[] pages{
tabPage1, tabPage2});
// Remove all the contents
tab.TabPages.Clear();
// Add a single entry
tab.TabPages.Add(tabPage1);
// Insert a single entry at a given position
tab.TabPages.Insert(0, tabPage2);
// Use index accessor for retrieving a page
// reference
Console.WriteLine("TabPages[0] name = {0}",
tab.TabPages[0].Text);
// Test if the collection contains an entry
Console.WriteLine("Contains tabPage1 = {0}",
tab.TabPages.Contains(tabPage1));
// Gain access to a Page by its Title
Console.WriteLine("Contains a named page? {0}",
tab.TabPages["Page1"] != null);
// Remove by object reference
tab.TabPages.Remove(tabPage1);
// Remove by index
tab.TabPages.RemoveAt(0);

Appearance

The first property you should modify is the Appearance which, as the
name suggests, is used to define the appearance of the control. When the value
of this property is changed it will automatically alter the values of other
properties to the defaults for the defined appearance. Therefore this is the
first property to be set otherwise previous changes to other properties will be
overwritten once this is set.

The three possible values for the property are VisualAppearance.MultiDocument,
VisualAppearance.MultiForm and VisualAppearance.MultiBox. The
first of these will place the page tabs at the top of the control and show both
arrow and close buttons. An example use of this would be to control which of a
group of documents is displayed. For example, use this appearance to achieve
the same effect as the Visual Studio .NET control that contains the open
documents.

The second appearance VisualAppearance.MultiForm places the page tabs at
the bottom of the control and does not show any buttons. Instead all the tabs
are sized to fit within whatever width the control is defined as. Use this
appearance to mimic the style of tab control seen in Visual Studio .NET inside
docking windows.

Use the third appearance VisualAppearance.MultiBox to create the high
contrast appearance you get when editing an HTML document in Visual Studio
.NET, where the document has two options at the bottom of the window.

Once the appearance has been set the developer can still modify the other
properties to alter the exact look and feel wanted. For example, the PositionTop
property could be used to shift the default position of the tabs for the MultiForm
appearance from the bottom of the control to the top. Use the supplied sample
project to experiment and achieve the look required.

Notifications

There are several possible events that can be generated by the TabControl
but only two are of interest to most developers. These are the ClosePressed
event that gets fired when the user presses the close button, and SelectionChanged
which notifies a change in the current selection.

Extra Features

Although you can use the control to achieve the same look and feel as the Visual Studio .NET TabControl, it
also has a couple of extra goodies. You can use the Multiline property to get a multi lines display
of tabs which you will not see in the VS.NET version. Another interesting feature is the HideTabsMode
property. You can use this to decide when the tabs area is made visible/invisible. For example, you can have
the tabs hidden unless the mouse is hovering over the control. Check out the sample to play around with this
and the other available options.

Revision History

30 Sept 2002 - Initial Revision

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

I am a big fan of .NET and have been working on developing a free user interface library to enhance the very basic controls that come out-of-the-box. Download the free source code project from http://www.dotnetmagic.com. I often carry out bespoke development work for companies, so feel free to email me for a quote on your .NET needs!