User Controls in Windows Form Using F#

Introduction

In this article I have explained the basic user controls, such as Label, TextBox, MonthCalendar, ComboBox, CheckBox, RadioButton and how to create these controls in Windows Forms applications.

Now  I want to show you how to create basic user controls in Windows Forms applications. Let's use the following procedure.

Step 1:

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

CreateApplicaiton

Step 2:

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

SelectReferences

 

AddReferences

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.".

ImportNamespaces

Step 4:

Label

A Label control allows users to display the text information on the form. You cannot click on the label or in this control you cannot fire any onclick event.

Write the following code for the User Control Label in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing 

let lblform=new Form(Text="Use Label")

let userlabel=new Label(Top=40,Left=60,Width=180)

userlabel.Text<-"Hi this is a label"

userlabel.ForeColor<-Color.Blue

lblform.Controls.Add(userlabel)

Application.Run(lblform)


Step 5:

Debug the application by pressing F5 to execute a Windows Forms application. After debugging the application the output will be as in the following figure.

UseLabel

Step 6:

TextBox

The TextBox control is generally used to get an input value from the user; it's a kind of input box that retrieves the value, amount or any name whatever you want to enter.

Write the following code for the User Control TextBox in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing 

let txtboxform=new Form(Text="Use TextBox")

Let usertxtbox=new TextBox(Top=40,Left=60,Width=180,Height=40,BorderStyle=BorderStyle.FixedSingle)

usertxtbox.Text<-"Hi this is a TextBox"

usertxtbox.ForeColor<-Color.Blue

txtboxform.Controls.Add(usertxtbox)

Application.Run(txtboxform)

 

Step 7 :

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseTextBox

Step 8:

TimerControl

The TimerControl allows the user to set the time interval to periodically execute an event at a specific time interval.

Write the following code for the User Control Timer in the F# editor.

open System  

open System.Drawing   

open System.Windows.Forms  

let timerform=new Form(Text="Use Timer")  

let exitbutton=new Button(Text="Exit", Location=new System.Drawing.Point(200, 220))  

let random=new Random()   

let timer=new Timer(Interval=1000,Enabled=true)  

timer.Tick.Add(fun time->timerform.BackColor<-Color.FromArgb(random.Next(0,255),random.Next(0,255),random.Next(0,255)))  

timerform.Controls.Add(exitbutton)  

exitbutton.Click.Add(fun quit->  

timer.Stop()  

timerform.Close())                     

timerform.Show()  

Application.Run(timerform) 

Step 9:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseTimerControl

Step 10:

Button

A Button control allows the user to post the form or fire an event, either client-side or server-side.

Write the following code for the User Control Button in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing 

let buttonform=new Form(Text="Use Button")

let userbutton=new Button(Top=40,Left=60,Width=60,Height=20)

userbutton.Text<-"Ok"

userbutton.BackColor<-Color.Ivory

buttonform.Controls.Add(userbutton)

Application.Run(buttonform)

Step 11:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseButton

Step 12:

MonthCalendar

The MonthCalendar control presents a graphical interface for users that allows them to view the calendar.

Write the following code for the User Control MonthCalendar in the F# editor.

open System  

open System.Drawing  

open System.Windows.Forms   

let calendarform=new Form(Text="Use MonthCalendar")  

let calendar=new MonthCalendar(MinDate=Convert.ToDateTime("1 Jan,1999"),MaxDate=Convert.ToDateTime("1 Jan,8013"),Top=40,Left=60)    

calendar.TitleBackColor<-Color.Blue

calendar.BackColor<-Color.DarkGray

calendarform.Controls.Add(calendar)  

calendarform.Show()  

Application.Run(calendarform)  

Step 13:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UserCalender

Step 14:

ComboBox

The ComboBox control allows users to select a single value at a time. The ComboBox contains the items. These items are in a drop-down list; users can select one item at a time.

Write the following code for the User Control ComboBox in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing  

let comboboxform=new Form(Text="Use ComboBox")

let items=[|"Delhi";"Noida";"Gurgaon";"Hariyana";"Ghaziabad";"Gr.Noida"|]

let combobox=new ComboBox(Top=40,Left=60,DataSource=items)

comboboxform.Controls.Add(combobox)

Application.Run(comboboxform)


Step 15:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseComboBox

Step 16:

RadioButton

The RadioButton control allows users to select a single option from multiple options.

Write the following code for the User Control RadioButton in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing 

let radiobuttonform=new Form(Text="Use RadioButton")

let radio1=new RadioButton(Text="Male",Top=50,Left=90) 

let radio2=new RadioButton(Text="Female",Top=70,Left=90)

radiobuttonform.Controls.Add(radio1)

radiobuttonform.Controls.Add(radio2)

Application.Run(radiobuttonform)


Step 17:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseRadioButton

Step 18:

ListBox

The ListBox control is used to display a set of values on the form.

Write the following code for the User Control ListBox in the F# editor.

open System  

open System.Drawing  

open System.Windows.Forms  

let listboxform=new Form(Text="Use Listbox")  

let programminglistbox=new ListBox(Sorted=true,Top=40,Left=60)   

listboxform.Load.Add(fun i->  

                    programminglistbox.Items.Add("F# programming")|>ignore  

                    programminglistbox.Items.Add("C# programming")|>ignore  

                    programminglistbox.Items.Add("C programming")|>ignore  

                    programminglistbox.Items.Add("Java programming")|>ignore  

                    programminglistbox.Items.Add("C++ programming")|>ignore)          

listboxform.Controls.Add(programminglistbox)             

listboxform.Show()  

Application.Run(listboxform)  


Step 19:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

ListBox

Step 20:

CheckBox

The CheckBox control allows selection of one or more options at a time. Users can check any of more than one from the set of values.

Write the following code for the User Control CheckBox in the F# editor.

open System

open System.Windows.Forms 

open System.Drawing 

let checkboxform=new Form(Text="Use CheckBox")

let checkboxcontrol=new CheckBox(Top=40,Left=60)

let checkboxcontrol2=new CheckBox(Top=40,Left=180)

let checkboxcontrol3=new CheckBox(Top=70,Left=60)

let checkboxcontrol4=new CheckBox(Top=70,Left=180)

checkboxcontrol.Text<-"CheckBox"

checkboxcontrol2.Text<-"CheckBox2"

checkboxcontrol3.Text<-"CheckBox3"

checkboxcontrol4.Text<-"CheckBox4"

checkboxform.Controls.Add(checkboxcontrol)

checkboxform.Controls.Add(checkboxcontrol2)

checkboxform.Controls.Add(checkboxcontrol3)

checkboxform.Controls.Add(checkboxcontrol4)

Application.Run(checkboxform)


Step 21:

Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure.

UseCheckBox

Summary

This article has explained the basic user controls, such as Label, TextBox, ComboBox, RadioButton and so on and how to use these controls in a Windows Forms application.

Up Next
    Ebook Download
    View all
    Learn
    View all