WebBrowser Control in F#

Introduction

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

Properties Description
AccessibleRole Get/set the accessible role of the control
BindingContext Get/set the BindingContext for the control
AccessibilityObject Get the AccessibilityObject assigned to the control
CanFocus Get a value indicating whether the control can receive focus
CanSelect Get a value indicating whether the control can be selected
Container Get the IContainer that contains the component
DataBindings Get the data bindings for the control
DefaultCursor Get/set the default cursor for the control
DefaultSize Get the default size of the control
DocumentTitle Get the title of document currently Displayed in the webBrowser control
Height Get/set the height of the control
Handle Get the window handle that the control is bound to
FontColor 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.
New Project Dialog Box

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

Step3- Now write the following code in the Program.fs window; your window will look like below.
WebBrowser Example
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
form.Controls.Add(container)
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)
content.Refresh()
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
try content.Url <- System.Uri(address.Text)
            with _ -> ())
 
form.Show()
#if COMPILED
[<STAThread()>]
Application.Run(form)
#endif
Step4- Now press F5 to execute the code.
Output
WebBrowser Output1
WebBrowser Output2
WebBrowser Output3

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

Up Next
    Ebook Download
    View all
    Learn
    View all