ListBox Control( Visual F#’s Windows Form Application)

Listbox are normally used to present a list of data to the user. To create a listbox, use the following syntax:
Let listboxobjvariable=new Listbox()
For instance:
Let listbox=new Listbox()
One of the most important methods of the ListBox object is the Add method which allows user to add an item into the listbox. Add method has the following syntax:
listboxobjvariable=new ListBox()
For instance:
listbox.Items.Add(“Hyberdabad”)
There are several usable properties and methods of the listbox object apart from the add method which includes SelectedItem(displays the listbox item text),SelectedIndex(returns the index number of the selected item),Count(counts the number of items),RemoveAt(Deletes an item),and Clear(wipes out the contents of the listbox). In the preceding example, we will just be using the Add method. Just follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do Step 3 again and select System.Drawing from the .Net tab.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
//use the f# standard library
open System
//specify the memory location of the classes used in drawing objects
//required to draw the listbox item text
open System.Drawing
//specify the location of the form class
open System.Windows.Forms
//creates a form and assign a Use listbox function to it
let sampleform=new Form(Text="Use Listbox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a label and set its Text to “Count”
let lbl=new Label(Text="Country:", Location=new System.Drawing.Point(20,10),AutoSize=true)
//makes a listbox
let countrylistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)
//adds an item to the listbox when the form is loaded
sampleform.Load.Add(fun i->
     //adds the items and ignore the passed index position values
                    countrylistbox.Items.Add("United States")|>ignore
                    countrylistbox.Items.Add("Philippines")|>ignore
                    countrylistbox.Items.Add("India")|>ignore
                    countrylistbox.Items.Add("Lithuania")|>ignore
                    countrylistbox.Items.Add("Germany")|>ignore
                    countrylistbox.Items.Add("Indonesia")|>ignore)
//displays the label to our form
sampleform.Controls.Add(lbl)      
//adds the listbox to our form    
sampleform.Controls.Add(countrylistbox)           
sampleform.Show()
//executes the application
Application.Run(sampleform)

5. Click the run icon to execute your application. You should now see an output similar to the following screen shot:




If you want a more in depth explanation on this controls, visit the MSDN website at msdn.microsoft.com or the Microsoft F# Development Center at http://msdn.microsoft.com/en-us/fsharp/default.aspx

Check box, Radio button, and Group box in Visual F#'s Windows Form Application

One of the most commonly used form controls in real-life application is the check box. A check box is a control that allows user to select various options from a group of options. To create a check box in Visual F#, use the following syntax:

let checkboxobjvariable=new CheckBox()

For instance:
let chk=new CheckBox()
Some of the essential properties of check box are Text and Checked. The Text property enables user to add a text beside a check box while the Checked property checks or unchecks a check box and can handle the boolean value true or false. Visual F#’s check box shares the same properties with the check boxes in other visual programming languages such as Visual C++, Visual J# and Visual Basic, so if you want to learn its other properties, check the check box properties of other visual programming languages.

Radio button of the other hand, is the complete opposite of check box for the fact that it only allow a single selection among a list of options. To change the text beside the radio button, use the Text property. It also has a Checked property which enables user to tick or untick the radio button.

To create a radio button, use the following syntax:
let radiobuttonobjvariable=new RadioButton()
For example:
let rdo=new RadioButton()
To group check boxes and radio buttons, a control called group box is used. Though there are several properties of radio buttons, the most frequently used is the Text property and is used to change the group box header text. To create a group box, use the following syntax:
Let groupboxobjvariable=new GroupBox()
For instance:
Let grp=new GroupBox()
Let’s now use the check box, radio button, and group box in a single application. Just follow these steps:

1. Click Start>All Programs>Microsoft Visual Studio 2008>Microsoft Visual Studio 2008.

2. Click File>New>Project>Select Visual F# in the project types>Select F# application in the Visual Studio installed templates category.

3. Click the Project menu>Add reference>Click the .Net tab>Locate then double-click System.Windows.Forms.
Do step 3 again and this time, select System.Drawing

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
// Learn more about F# at http://fsharp.net
//use the Visual F# library
open System
//use System.Drawing
open System.Drawing
//specifies the location of the Form classes
open System.Windows.Forms
//creates a form named cnbform and assign a “Use Radio Button” caption to it
let cnbform=new Form(Text="Use Radio Button",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
//creates a group box and sets its header text to “RadioButtons”
//Use the Top and Left properties to align it in the form
let gbox1=new GroupBox(Text="RadioButtons", Location=new System.Drawing.Point(10,5),Height=80)
//creates a radio button and change its text to “First radio button”
//Use the top and left property to align it in the group box
let radio1=new RadioButton(Text="First radio button", Location=new System.Drawing.Point(20,20),AutoSize=true)
//creates radio button and set its text to “Second radio button”
//Use the autosize property to automatically expand the width of text handler
let radio2=new RadioButton(Text="Second radio button", Location=new System.Drawing.Point(20,40),AutoSize=true)
//creates a group box and set its text to “CheckBoxes”
//position the group box in the form by using the Top and Left properties
let gbox2=new GroupBox(Text="CheckBoxes",Height=80, Location=new System.Drawing.Point(10,100))
//creates a check box and change its text to “First checkbox”
//Use the top and left property to align it in the group box
let check1=new CheckBox(Text="First checkbox", Location=new System.Drawing.Point(20,20),AutoSize=true)
//creates another check box and change its text to “Second checkbox”
//Use the top and left property to align it in the group box

let check2=new CheckBox(Text="Second checkbox", Location=new System.Drawing.Point(20,40),AutoSize=true)
//creates a button and set its text to “Exit”
let exitbutton=new Button(Location=new System.Drawing.Point(200,200),Text="Exit")

//displays the first group box into the form
cnbform.Controls.Add(gbox1)
//adds the radio buttons into the groupbox
gbox1.Controls.Add(radio1)
gbox1.Controls.Add(radio2)

//adds the second group box into the form
cnbform.Controls.Add(gbox2)
//displays the checkboxes into the second groupbox
gbox2.Controls.Add(check1)
gbox2.Controls.Add(check2)

//inserts the exit button into the form
cnbform.Controls.Add(exitbutton)
//attach a click event to the first radio button then assign a function to it
radio1.Click.Add(fun r1msg->
//check the first checkbox
check1.Checked<-true
               //unchecks the first checkbox
                check2.Checked<-false)

//attach a click event to the first radio button then assign a function to it

radio2.Click.Add(fun r2msg->
//check the second checkbox
check2.Checked<-true
              //unchecks the first checkbox
              check1.Checked<-false)
//quits the form when the exit button is clicked                 
exitbutton.Click.Add(fun bact->cnbform.Close())    
//shows the form       
cnbform.Show()
//runs the application
Application.Run(cnbform)
5. Lets view of our again once again and this time, comments omitted:

// Learn more about F# at http://fsharp.net
open System
open System.Drawing
open System.Windows.Forms
let cnbform=new Form(Text="Use Radio Button",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)

let gbox1=new GroupBox(Text="RadioButtons", Location=new System.Drawing.Point(10,5),Height=80)
let radio1=new RadioButton(Text="First radio button", Location=new System.Drawing.Point(20,20),AutoSize=true)
let radio2=new RadioButton(Text="Second radio button", Location=new System.Drawing.Point(20,40),AutoSize=true)

let gbox2=new GroupBox(Text="CheckBoxes",Height=80, Location=new System.Drawing.Point(10,100))
let check1=new CheckBox(Text="First checkbox", Location=new System.Drawing.Point(20,20),AutoSize=true)
let check2=new CheckBox(Text="Second checkbox", Location=new System.Drawing.Point(20,40),AutoSize=true)

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


cnbform.Controls.Add(gbox1)
gbox1.Controls.Add(radio1)
gbox1.Controls.Add(radio2)

cnbform.Controls.Add(gbox2)
gbox2.Controls.Add(check1)
gbox2.Controls.Add(check2)

cnbform.Controls.Add(exitbutton)

radio1.Click.Add(fun r1msg->
check1.Checked<-true
                    check2.Checked<-false)
radio2.Click.Add(fun r2msg->
check2.Checked<-true
                    check1.Checked<-false)
                    
exitbutton.Click.Add(fun bact->cnbform.Close())           
cnbform.Show()
Application.Run(cnbform)

6. Click the run icon to execute your application. You should now see an output similar to the following screen shot: