Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

WebBrowser Control in F-Sharp

Viewed:  2577 
Posted On:  05/10/2012 22:21:06 
This article is a demonstration of the WebBrowser control and its properties in F#. Have a quick review to learn.  


Today I am going to explain use of the WebBrowser control in F#. The WebBrowser control is essentially a managed wrapper around some COM interface that binds to Internet Explorer and provides us several capabilities. GUI applications revolve around events, and F# provides a natural way to process events with function. You can add event handlers for a WebBrowser like a status bar, Menu bar, Toolbar, Address combo box and Go Button. WebBrowser control is not one of the controls that come by default in the control box.

WebBrowser control is used to perform many of the tasks like navigating to a site, Making Page on startup, save page, open page and auto submit etc. WebBrowser control can easily be extended by adding other interface and methods. WebBrowser control use the control as an MSHTML editor. WebBrowser control also exposes some interesting events that allow a programmer to react when a document is loaded, navigation is performed etc.

First of all a user can use a WebBrowser control to display a web page in a Windows Forms application. WebBrowser Activex control used to send data by using the post method to an HTTP server like Microsoft internet information server. This control exposes an API that allows an application to embed and control a browser rendering engine. WebBrowser libraray has a Mono. Mozilla namespace, which implements the public Mono.WebBrowser interfaces.

Properties of WebBrowser




Get/set the accessible role of the control


Get/set the BindingContext for the control


Get the AccessibilityObject assigned to the control


Get a value indicating whether the control can receive focus


Get a value indicating whether the control can be selected


Get the IContainer that contains the component


Get the data bindings for the control


Get/set the default cursor for the control


Get the default size of the control


Get the title of document currently Displayed in the webBrowser control


Get/set the height of the control


Get the window handle that the control is bound to


Get/set the height of the font of the control

Getting Started
Now we are discussing an example of WebBrowser in F#. Here we are using WebBrowser control with two Buttons named back and Forward. Back Button is used to go back on any page displaying the WebBrowser control and the Forward Button is to go Forward to any Page.
Step1- First, open a new F# project using Visual Studio 2010, and give a name to it.


Figure 1.

Step2- In Solution Explorer click on Program.fs file. 


Figure 2.

Step3- Now write the following code in the Program.fs window; your window will look like below.


Figure 3.

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

let form = new Form(Visible=true, Text="A Web Browser control")
let container = new TableLayoutPanel(ColumnCount=2, RowCount=3)
let label = new Label(Text="Address:")
let address = new TextBox()
let toolbar = new ToolStrip()
let content = new WebBrowser()
let back = new ToolStripButton("Previous")
let forward = new ToolStripButton("Next")
label.TextAlign <- ContentAlignment.MiddleRight
form.Width <- 1024
form.Height <- 768
container.Dock <- DockStyle.Fill
address.Dock <- DockStyle.Fill
content.Dock <- DockStyle.Fill
toolbar.Items.Add(back) |> ignore
toolbar.Items.Add(forward) |> ignore
container.Controls.Add(label, 0, 0)
container.Controls.Add(address, 1, 0)
container.Controls.Add(toolbar, 0, 1)
container.Controls.Add(content, 0, 2)
container.SetColumnSpan(toolbar, 2)
container.SetColumnSpan(content, 2)
back.Click.Add(fun _ -> content.GoBack() |> ignore)
forward.Click.Add(fun _ -> content.GoForward() |> ignore)
address.KeyDown.Add(fun e -> if e.KeyCode = Keys.Enter then
content.Url <- System.Uri(address.Text)
            with _ -> ())

Step4- Now press F5 to execute the code.


Figure 4.


Figure 5.

In this article I have discussed about the WebBrowser control in F#.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us