In this tutorial I will show how to create a simple web browser application in C# using WinForms. I should mention, that in this project is used the WebBrowser control, which is a part of Internet Explorer. In this tutorial I also will use some XML, so you need at least a basic understanding of what it is.
Special Tutorial Requirements:* C# IDE (Visual Studio 2008 used in this tutorial)
* .NET Framework 2.0
So, here we go.
1. Create a C# Windows Forms application:
2. Add a WebBrowser component to your form:
3. Change the WebBrowser control's Dock property to None:
4. Set the WebBrowser control's Anchor property to Top, Bottom, Left, Right (in this case the control will be resized with the form). As you see on the picture, it is set just to Top, Bottom, Right. This is because my form is resized only to the right, but you can change this property to other values as well.
5. Add a ToolStrip control to your form:
6. Add some buttons to the ToolStrip control (we'll need 5 buttons).
7. Set the DisplayStyle property of every added button to Text (you can change the style to one you like most).8. Set the Text property of the buttons to the following: - <<BACK
- FORWARD>>
- REFRESH
- STOP
- HOME
9. Add a ListBox control to your form:
10. Anchor the ListBox control to Top, Right, Bottom:
11. Add a text field to the ToolStrip and a button with the Text property set to GO:
12. Change the Width property of the text field to 300 (so the URL can be fully viewable):
13. Add 2 buttons at the top of the ListBox (Add and Remove):
So, the form looks like this:

Now, let's pass to the code:1. Declare the System.Xml namespace:
csharp
2. Declare the variable [b]Home as string. It will keep the home page from the XML.[/b]
CODE
public string Home;
3. Code for the Form1_Load event:
csharp
// Creates a closing event for the form.
this.Closing += new CancelEventHandler(Form1_Closing);
listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick);
XmlDocument loadDoc = new XmlDocument();
loadDoc.Load(Application.StartupPath + "\\load.xml");
Home = loadDoc.SelectSingleNode("/browser/home").Attributes["url"].InnerText;
webBrowser1.Navigate(Home);
foreach(XmlNode favNode in loadDoc.SelectNodes("/browser/favorites/item"))
listBox1.Items.Add(favNode.Attributes["url"].InnerText);
The XML document is in the following format:
CODE
<browser>
<home url='http://www.dreamincode.net' />
<favorites>
<item url='www.ymail.com' />
<item url='www.dreamincode.net' />
</favorites>
</browser>
The file name is
load.xml and the file is located in the same folder, as the executable. You can change the file path to the one you want. The ListBox is populated with favorites from the XML.
4. Code for the "<<BACK" button:
csharp
private void toolStripButton1_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
5. Code for the "FORWARD>>" button:CODE
private void toolStripButton2_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
6. Code for the "REFRESH" button:
csharp
private void toolStripButton3_Click(object sender, EventArgs e)
{
webBrowser1.Refresh();
}
7. Code for the "STOP" button:
csharp
private void toolStripButton4_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
8. Code for the "HOME" button:
csharp
private void toolStripButton5_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(Home);
}
Remember I declared the
Home variable as string? Now the browser will navigate to that URL if the "HOME" button is pressed.
9. The code for the listBox1_MouseDoubleClick (this event handler was declared in the Form1_Load event):
csharp
private void listBox1_MouseDoubleClick(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
webBrowser1.Navigate(listBox1.SelectedItem.ToString());
}
10. The code for the "Add" button:
csharp
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(webBrowser1.Url.OriginalString);
}
11. The code for the "Remove" button:
csharp
private void button2_Click(object sender, EventArgs e)
{
// Removes the selected favorites entry.
listBox1.Items.Remove(listBox1.SelectedItem);
}
12. Now we need to program the Closing event of our form:
csharp
private void Form1_Closing(object sender, CancelEventArgs e)
{
XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + "\\load.xml", null);
writer.WriteStartElement("browser");
writer.WriteStartElement("home");
writer.WriteAttributeString("url", "http://www.dreamincode.net");
writer.WriteEndElement();
writer.WriteStartElement("favorites");
for (int i=0; i < listBox1.Items.Count; i++ )
{
writer.WriteStartElement("item");
writer.WriteAttributeString("url", listBox1.Items[i].ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
}
This one writes the stored favorites to the above mentioned
load.xml using
XmlTextWriter. If you look closer at the format of the file, you will see that this code writes the home page for our browser and every item in the favorites list (which is being parsed item-by-item).
Congratulations!
You've just created your own IE-based web browser!
Of course this tutorial shows only the basics of creating such kind of applications, but it gives an essential understanding on how the WebBrowser control works.