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:
  1. Let listboxobjvariable=new Listbox()  
For instance:
  1. 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:
  1. listboxobjvariable=new ListBox()  
For instance:
  1. 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 “:
  1. //use the f# standard library  
  2. open System  
  3. //specify the memory location of the classes used in drawing objects  
  4. //required to draw the listbox item text  
  5. open System.Drawing  
  6. //specify the location of the form class  
  7. open System.Windows.Forms  
  8. //creates a form and assign a Use listbox function to it  
  9. let sampleform=new Form(Text="Use Listbox",StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font)  
  10. //creates a label and set its Text to “Count”  
  11. let lbl=new Label(Text="Country:", Location=new System.Drawing.Point(20,10),AutoSize=true)  
  12. //makes a listbox  
  13. let countrylistbox=new ListBox(Sorted=true,Location=new System.Drawing.Point(20,30),FormattingEnabled=true)  
  14. //adds an item to the listbox when the form is loaded  
  15. sampleform.Load.Add(fun i->  
  16.      //adds the items and ignore the passed index position values  
  17.                     countrylistbox.Items.Add("United States")|>ignore  
  18.                     countrylistbox.Items.Add("Philippines")|>ignore  
  19.                     countrylistbox.Items.Add("India")|>ignore  
  20.                     countrylistbox.Items.Add("Lithuania")|>ignore  
  21.                     countrylistbox.Items.Add("Germany")|>ignore  
  22.                     countrylistbox.Items.Add("Indonesia")|>ignore)  
  23. //displays the label to our form  
  24. sampleform.Controls.Add(lbl)        
  25. //adds the listbox to our form      
  26. sampleform.Controls.Add(countrylistbox)             
  27. sampleform.Show()  
  28. //executes the application  
  29. 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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.