Getting Started With TabControl in F#

Introduction

In this article you'll see how to create a TabControl. The .NET Framework provides the versatile server Control in Window Forms applications to manage the user layout. This article explains how to add the TabControl to a Windows Forms form. If your application is complex enough to need to arrange the information then you can use the TabPage property to add a number of tabs to the TabControl Control.

TabControl Class

The TabControl Class provides multiple tabs at a time in a single Windows Forms form. You can manage your information in the application. Each tab contains the specific information that is easy to manage the data in the application. A TabControl is just like a container that holds the data in a systematic way. Once the TabControl control is defined with its properties, we need to add it to a Windows Forms form by calling the "Form.Controls.Add" method.

Namespace for TabControl Class: System.Windows.Forms.TabControl

TabPage Property

The TabPage Property provides the list of tabs in the TabControl. The TabPage Property has the ability to add a new tab, remove a tab and set the order of tabs in the application.

Namespace for Tappage: System.Windows.Forms.TabPage

Now let's use the following procedure.

Step 1:

Open Visual Studio then select "Create New Project" --> "F# Console Application".

create-app.jpg

Step 2:

Now go to the Solution Explorer on the right side of the application. Select the references and right-click on it and select "Add references".

add-ref.jpg

Step 3:

After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and "System.Drawing" while holding down the "Ctrl" key and click on "Ok".

import-namespace.jpg

Step 4:

Provide the following code for the F# application:

// Learn more about F# at http://fsharp.net 

//use the F# library 

open System 

//use the drawing classes 

open System.Drawing 

//specifies the location of the form class 

open System.Windows.Forms 

//specifies the font face and style 

let ffont=new Font("Arial", 9.75F,FontStyle.Italic, GraphicsUnit.Point) 

//creates a form 

let tabform=new Form(Text="Use TabControl",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,ClientSize=new System.Drawing.Size(350,350),FormBorderStyle=FormBorderStyle.FixedSingle) 

//creates a tab control set its TabSizeMode to fixed 

let tabcontrol=new TabControl(Location=new System.Drawing.Point(20, 20),Size=new System.Drawing.Size(214, 192)) 

//creates a new tab page 

let page1=new TabPage(Text="Monday",Cursor=Cursors.Hand) 

//creates a new label 

let page1label=new Label(Text="Monday Work",Font=ffont,Location=new System.Drawing.Point(20, 20)) 

//creates another tab page 

let page2=new TabPage(Text="Tuesday",Cursor=Cursors.Hand) 

//creates another label 

let page2label=new Label(Text="Tuesday Work",Font=ffont,Location=new System.Drawing.Point(20, 20))

//creates a new tab page 

let page3=new TabPage(Text="Wednesday",Cursor=Cursors.Hand) 

//creates a new label 

let page3label=new Label(Text="Wed Work",Font=ffont,Location=new System.Drawing.Point(20, 20)) 

//creates a new tab page 

let page4=new TabPage(Text="Thursday",Cursor=Cursors.Hand) 

//creates a new label 

let page4label=new Label(Text="Thursday Work",Font=ffont,Location=new System.Drawing.Point(20, 20)) 

//creates a new tab page 

let page5=new TabPage(Text="Friday",Cursor=Cursors.Hand) 

//creates a new label 

let page5label=new Label(Text="Friday Work",Font=ffont,Location=new System.Drawing.Point(20, 20)) 

//creates a new tab page 

let page6=new TabPage(Text="Saturday",Cursor=Cursors.Hand) 

//creates a new label 

let page6label=new Label(Text="Saturday Work",Font=ffont,Location=new System.Drawing.Point(20, 20)) 

//adds the tabcontrol to our form 

tabform.Controls.Add(tabcontrol) 

//adds the tab pages to our tab control 

tabcontrol.Controls.Add(page1) 

//adds the label to the first page 

page1.Controls.Add(page1label) 

//adds the second page to our tab control 

tabcontrol.Controls.Add(page2) 

//adds the second label to our tab control 

page2.Controls.Add(page2label) 

//adds the tab pages to our tab control 

tabcontrol.Controls.Add(page3) 

//adds the label to the 3rd page 

page3.Controls.Add(page3label) 

tabcontrol.Controls.Add(page4) 

//adds the label to the 4th page 

page4.Controls.Add(page4label) 

tabcontrol.Controls.Add(page5) 

//adds the label to the 5th page 

page5.Controls.Add(page5label) 

tabcontrol.Controls.Add(page6) 

//adds the label to the 6th page 

page6.Controls.Add(page6label) 

tabform.Show() 

//executes our application 

Application.Run(tabform) 

Step 5:

Debug the application by pressing F5 and the result will appear in the application as in the following: figure:

aftr-debug.jpg

Step 6:

Now you can select any tab by clicking on the tabcontrol.

tue-wrk.jpg

Step 7:

If you want to check the Saturday work then just click on the Saturday Tab in the figure given below.

sat-tab.jpg

Summary

In this article, we discussed how to create and use a TabControl control. First we discussed how to create a TabControl and add tab pages to the TabControl. I hope it will help you to understand the TabControl control.

Up Next
    Ebook Download
    View all
    Learn
    View all