1. Computing & Technology

Discuss in my forum

C# Programming Tutorial - Programming Advanced Winforms in C#

By , About.com Guide

10 of 10

Working With TabTabControls
Tbe Two Tabs TabControl

A TabControl is a handy way to save form space by having multiple tabs. Each tab can have an icon or text and you can select any tab and display its controls. The TabControl is a container but it only contains TabPages. Each TabPage is also a container that can have normal controls added to it.

In example x7.cs, I've created a two tab page panel with the first tab called Controls having three buttons and a checkbox on it. The second tab page is labeled Logs and used to display all the logged actions which includes clicking a button or toggling a check box. A method called Log() is called to log every button click etc. It adds the supplied string to a ListBox.

I've also added two right click popup menus items to the TabControl in the usual way. First add a ContextMenuStrip to the form and set it in the ContextStripMenu property of the TabControl. The two menu choices are Add New Page and Remove This Page. However I've restricted the Page removal so only newly added tab pages can be removed and not the original two.

Adding a New Tab Page

This is easy, just create a new tab page, give it a Text caption for the Tab then add it to the TabPages collection of the Tabs TabControl

TabPage newPage = new TabPage();
newPage.Text = "New Page";
Tabs.TabPages.Add(newPage);

In the ex7.cs code I've also created a label and added that to the TabPage. The code was obtained by adding it in the Form designer to create the code then copying it.

Removing a page is just a matter of calling TabPages.RemoveAt(), using the Tabs.SelectedIndex to get the currently selected Tab.

Conclusion

In this tutorial we've seen how some of the more sophisticated controls work and how to use them. In the next tutorial I'm going to continue with the GUI theme and look at the background worker thread and show how to use it.

©2012 About.com. All rights reserved.

A part of The New York Times Company.