ListBoxes in F#

Introduction

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  

//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.Items.Add("c-sharpcorner")

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
                    sitelistbox.Items.Add("c-sharpcorner")|>ignore
                    sitelistbox.Items.Add("vbdotnetheaven")|>ignore
                    sitelistbox.Items.Add("dotnetheaven")|>ignore
                    sitelistbox.Items.Add("dbtalks")|>ignore
                    sitelistbox.Items.Add("longhorncorner")|>ignore
                    sitelistbox.Items.Add("Mindcracker")|>ignore)
            //displays the label to our form
            sampleform.Controls.Add(lbl)     
            //adds the listbox to our form   
            sampleform.Controls.Add(sitelistbox)          
            sampleform.Show()
            //executes the application
            Application.Run(sampleform)
Step4- Now press F5 to execute the Program.
Output
ListBox Output 1
ListBox Output 2

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

Up Next
    Ebook Download
    View all
    Learn
    View all