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

Posted by Rey Dacoco in

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:
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 “:
//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
//displays the label to our form
//adds the listbox to our form    
//executes the application

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 or the Microsoft F# Development Center at


Post a Comment


My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List


Got questions? You can reach me here: