Working With ComboBox in FSharp


Introduction: ComboBox is used to make a single selection from a list of items by user. Now we will create a ComboBox in FSharp application. The syntax for creating ComboBox is given below

Syntax For Creating ComboBox:
    let ComboBox_Name=new ComboBox()

Like,
let combo=new ComboBox()

Now we create a window form. For doing this we create a F# Application as show below

combobox in f#

We go to Solution Explorer and right Click on References. Then Click on .NET and select System.Drawing and System.Windows.Form and Click on ok button. Figure is given below

combobox in f#

Then, We write the following code.

open System
open
System.Windows.Forms
open
System.Drawing 
 

let
form=new Form()
form.Text<- "My Window Application"
form.BackColor <-Color.Gray
 

let
combo=new ComboBox()
 
form.Controls.Add(combo)
Application.Run(form)

Now we run the application. The output will look like the below figure

combobox in f#

This ComboBox has no items. Now we bind the ComboBox with a set of values. We write the following code

open System
open
System.Windows.Forms
open
System.Drawing 
 

let
form=new Form()
form.Text<- "My Window Application"
form.BackColor <-Color.Gray

let
items=[|"A";"B";"C";"D";"E";"F"|]
 

let
combo=new ComboBox(DataSource=items)
form.Controls.Add(combo)
Application.Run(form)

We run the application. The output will look like the below figure

combobox in f#

When we Click on the ComboBox, it shows all items, allowing the user to select one of them, like the below figure

combobox in f#


Similar Articles