Split data onscreen logically with VB.NET's Tab Control

VB.NET developers are often tasked with having to display a lot of necessary information on the screen with limited space. If you can split the data on the screen logically into different sections, you should consider using the Tab Control. In this tip, I will show you how to use the Tab Control to allow data to be logically split on the screen.

How to use the Tab Control

The Tab Control allows you to arrange data and other controls in a compact way. Users often like the Tab Control as well because screens with Tab Controls are fairly intuitive.

In order to use the Tab Control in your applications, open the Toolbox and locate Tab Control under the Containers section. Drag the Tab Control onto the form and then start adding other controls to it. For instance, if you want to display the Tab Control with three tabs and a TextBox on each tab, you would create three tabs and then add a text box for each tab. Depending on the application's specifics, you may want to set some properties in the design time, but you will be able to set the same properties and even add controls to the Tab Control in runtime if necessary.

The Tab Control is easy to use, and it provides a great deal of visual and styling flexibility. You can set the Tab Control's visual properties and apply styles to the individual parts of the control. Once you have the Tab Control on your form, the form will look like Figure A. Figure A

Figure A

The Tab Control is initially displayed with two tabs; you can add tabs either using the properties of the control in design time or with the code in run time. In the code example below, I add a third tab to the Tab Control and a control to each tab in run time. To set the control properties in design time and add and/or modify tabs, click the Tab Pages collection, and you will see a screen that looks like Figure B. Figure B

Figure B

        Dim txt1 As New System.Windows.Forms.TextBox

        Dim lbl1 As New System.Windows.Forms.Label

        lbl1.Text = "abc"

        Dim cbo1 As New System.Windows.Forms.ComboBox

        With cbo1




            cbo1.SelectedIndex = 0

        End With

        With TabControl1

            .TabPages.Item(0).Text = "General Info"

            .TabPages.Item(0).Name = .TabPages.Item(0).Text

            .TabPages.Item(1).Text = "Details"

            .TabPages.Item(1).Name = .TabPages.Item(1).Text


            .TabPages.Item(2).Name = .TabPages.Item(2).Text




        End With

Notes about the example

I define three controls that I would add to different TabPages of the Tab Control: txt1 as a TextBox, lbl1 as a Label, and cbo1 as a ComboBox. I then set the Text property of the Label control (to make it visible in the example) and add a few items to the ComboBox control. The prep work is done, and I am ready to use the properties of the Tab Control to set the Text and Name properties of its two tab pages, and then add a third tab page.

Finally, I add: the TextBox to the first tab, the Label to the second tab, and the ComboBox to the third tab. Since I assigned not only the Text but also the Name properties of each Tab Page, I was able to access them either by Item number as in Item(0) or by the Item name ("General Info").

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.


Get Visual Basic tips in your inbox Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!