Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 119,058 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,521 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Creating a simple web browser

 
Reply to this topicStart new topic

> Creating a simple web browser, How to create a simple web browser using C# and WinForms

PixelCard
Group Icon



post 10 Jul, 2008 - 02:47 PM
Post #1


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:

IPB Image

2. Add a WebBrowser component to your form:

IPB Image

3. Change the WebBrowser control's Dock property to None:

IPB Image

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.

IPB Image

5. Add a ToolStrip control to your form:

IPB Image

6. Add some buttons to the ToolStrip control (we'll need 5 buttons).

IPB Image

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:

IPB Image

10. Anchor the ListBox control to Top, Right, Bottom:

IPB Image

11. Add a text field to the ToolStrip and a button with the Text property set to GO:

IPB Image

12. Change the Width property of the text field to 300 (so the URL can be fully viewable):

IPB Image

13. Add 2 buttons at the top of the ListBox (Add and Remove):

IPB Image

So, the form looks like this:

IPB Image


Now, let's pass to the code:


1. Declare the System.Xml namespace:

csharp

using System.Xml;


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.


Attached File(s)
Attached File  browserApp.zip ( 48.81k ) Number of downloads: 168
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Kirth
*



post 11 Jul, 2008 - 05:31 AM
Post #2
Sorry if I'm wrong, but I think you forgot the code for the navigating itself!

csharp


// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}

private void toolStripButton6_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}



For the rest, nice tutorial with good and clearifying screenshots! Good work!
Go to the top of the page
+Quote Post

PixelCard
Group Icon



post 11 Jul, 2008 - 12:32 PM
Post #3
QUOTE(Kirth @ 11 Jul, 2008 - 05:31 AM) *

Sorry if I'm wrong, but I think you forgot the code for the navigating itself!

csharp


// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}

private void toolStripButton6_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(toolStripTextBox1.Text);
}



For the rest, nice tutorial with good and clearifying screenshots! Good work!


Thanks for reminding, because I really forgot about it.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/13/08 03:42PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month