WebBrowser control(Visual F# Windows Forms Application)

WebBrowser control is a control used to display a web page or web document. To create a WebBrowser control in F#, use the following syntax:

  1. let webbrowserobjvaribale=new WebBrowser()  
For instance:
  1. let browser=new WebBrowser()  
One of the important methods of the WebBrowser object is the Navigate method which is used to navigate and display the specified URL. Navigate method has the following syntax:
  1. Webbrowserobjvaribale.Navigate(“url”)  
For Example:
  1. Webbrowserobjvaribale.Navigate(“www.google.com”)  
The following example demonstrate a simple application using WebBrowser control:

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 this time select System.Drawing.

4. Enter the following code after the line “// Learn more about F# at http://fsharp.net “:
  1. // Learn more about F# at http://fsharp.net  
  2. //uses the standard F# library  
  3. open System  
  4. //use the drawing classes  
  5. open System.Drawing   
  6. //specify the namespace memory location of the form class  
  7. open System.Windows.Forms  
  8. //creates a new font  
  9. let myfont=new Font("Arial",8.0f,FontStyle.Regular,GraphicsUnit.Point)   
  10. //creates a new form  
  11. let webform=new Form(Text="Use WebBrowser",Width=400,StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,FormBorderStyle=FormBorderStyle.FixedSingle)  
  12. let addresslabel=new Label(Location=new System.Drawing.Point(15, 5),Text="Enter URL:",AutoSize=true)  
  13. let urltextbox=new TextBox(Location=new System.Drawing.Point(80, 5))  
  14. let openpgbutton=new Button(Text="Open Page",Location=new System.Drawing.Point(190, 5))  
  15. let opennpbutton=new Button(Text="New Window",Location=new System.Drawing.Point(280, 5),AutoSize=true)  
  16. //create a webbrowser control and change its dock property to none  
  17. //so that it will not occupy the whole form  
  18. let browser=new WebBrowser(Dock=DockStyle.None,Size=new System.Drawing.Size(300, 200),Location=new System.Drawing.Point(40, 30),MinimumSize=new System.Drawing.Size(20, 20))                 
  19. //changes the font to arial  
  20. webform.Font<-myfont  
  21. //adds the controls into our form  
  22. webform.Controls.Add(addresslabel)  
  23. webform.Controls.Add(urltextbox)  
  24. webform.Controls.Add(openpgbutton)  
  25. webform.Controls.Add(opennpbutton)  
  26. webform.Controls.Add(browser)  
  27. //when the form is loaded  
  28. webform.Load.Add(fun defa->  
  29.             //display the google page  
  30.             browser.Navigate("www.google.com",false))   
  31. //locate and navigate the webaddress inpputed in the textbox  
  32. openpgbutton.Click.Add(fun openpage->  
  33.             browser.Navigate(urltextbox.Text,false))  
  34. //opens the page in a new window  
  35. opennpbutton.Click.Add(fun openpageinnewwindow->  
  36.             browser.Navigate(urltextbox.Text,true))  
  37. webform.Show()  
  38. [<STAThread>]  
  39. //executes our application  
  40. Application.Run(webform)  

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

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