Reader Level:

ListBoxes in F#

By Dea Saddler on Jul 27, 2011
This article is a demonstration of ListBoxes and how to add items to a ListBox in F#. Have a quick review to learn.


Today I am going to explain about ListBoxes in F# and how to add items to a ListBox in our Windows form. ListBoxes are generally used to represent a list of items to the users. You can select items from a ListBox. The items in a ListBox can be removed as well as added. ListBoxes can be bound to a variety of data sources. The following is the syntax to create a ListBox in F#.


//syntax to create listbox
let listbox= new ListBox()

Add Items in ListBox

ListBox contains an Add method, which allows users to add items to the ListBox. You can call the Add method like below.

//add method

ListBox Properties

ListBox also contains some properties and methods other than the add method like selectedItems, SelectedIndex, Count, RemoveAt and Clear.

Properties Description
SelectedItem Get/set the selected Item of the List
SelectedIndex Get/set the Index of the selected Item
SelectedItems Get/set the currently selected Item for the ListBox
Template Get/set a control Template

ListBox Methods
Methods Description
RemoveAt Removes the Item at the specified Index
Clear Removes all Items
GetSelected Returns a value Indicating whether the specified item is selected
Focus Set input Focus to the control

What we have Done
We first create a form and assign a function to it, then we create a label and make a ListBox. Now we added items to the ListBox. Finally we display the Label and add the ListBox to the form. Here in our code we used |>ignore that is for throwing away the result of computation or ignore the passed value.
Getting Started with F#
Step1- Open a new project in F# using Visual studio 2010 and select the F# Windows app (winform) template. Give a name to project.
New Project dialog box

Step2- Now open the Solution Explorer and click on the Program.fs file.
Solution Explorer

Step3- Now write the following code in the Program.fs window; your window will look like below.
ListBox Example

namespace Listbox
open System
open System.Drawing
open System.Windows.Forms
module Main =
            //createing a form and My listbox function assigning to it
            let sampleform=new Form(Text="My ListBox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)
            //creating a label and setting text to "sites"
            let lbl=new Label(Text="Sites:", Location=new System.Drawing.Point(20,10),AutoSize=true)
            //makes a listbox
            let sitelistbox=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
            //displays the label to our form
            //adds the listbox to our form   
            //executes the application
Step4- Now press F5 to execute the Program.
ListBox Output 1
ListBox Output 2

In this article I have discussed how to use List Box In F#.



Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.
Infragistics jQuery Controls

Trending up