WebBrowser control(Visual F# Windows Forms Application)

Posted by Rey Dacoco in

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:

let webbrowserobjvaribale=new WebBrowser()
For instance:
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:
Webbrowserobjvaribale.Navigate(“url”)
For Example:
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 “:
// Learn more about F# at http://fsharp.net
//uses the standard F# library
open System
//use the drawing classes
open System.Drawing 
//specify the namespace memory location of the form class
open System.Windows.Forms
//creates a new font
let myfont=new Font("Arial",8.0f,FontStyle.Regular,GraphicsUnit.Point) 
//creates a new form
let webform=new Form(Text="Use WebBrowser",Width=400,StartPosition=FormStartPosition.CenterScreen,AutoScaleMode=AutoScaleMode.Font,FormBorderStyle=FormBorderStyle.FixedSingle)
let addresslabel=new Label(Location=new System.Drawing.Point(15, 5),Text="Enter URL:",AutoSize=true)
let urltextbox=new TextBox(Location=new System.Drawing.Point(80, 5))
let openpgbutton=new Button(Text="Open Page",Location=new System.Drawing.Point(190, 5))
let opennpbutton=new Button(Text="New Window",Location=new System.Drawing.Point(280, 5),AutoSize=true)
//create a webbrowser control and change its dock property to none
//so that it will not occupy the whole form
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))               
//changes the font to arial
webform.Font<-myfont
//adds the controls into our form
webform.Controls.Add(addresslabel)
webform.Controls.Add(urltextbox)
webform.Controls.Add(openpgbutton)
webform.Controls.Add(opennpbutton)
webform.Controls.Add(browser)
//when the form is loaded
webform.Load.Add(fun defa->
            //display the google page
            browser.Navigate("www.google.com",false)) 
//locate and navigate the webaddress inpputed in the textbox
openpgbutton.Click.Add(fun openpage->
            browser.Navigate(urltextbox.Text,false))
//opens the page in a new window
opennpbutton.Click.Add(fun openpageinnewwindow->
            browser.Navigate(urltextbox.Text,true))
webform.Show()
[<STAThread>]
//executes our application
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

0 comments

Post a Comment

Contributor

My photo
I am just a simple man with a simple mind. And this is my simple blog. New Posts will be available soon. Enjoy reading and never stop learning:)

Total Pageviews

Link List

Archives

Got questions? You can reach me here: