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:
The form then needs a tab control:
And the tab control will contain a number of tab pages:
The code will need to define any components to be used in the tabs:
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:
So, first the code adds the tab control to the form:
Then it adds tab pages to the tab control::
Finally the code adds any controls to the appropriate tab page:
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:
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:
Now the event handler will run a new function whenever the user changes the size of the form:
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.