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.
Resized to 49% (was 1024 x 768) - Click image to enlarge
5. Add a ToolStrip control to your form:
Resized to 49% (was 1024 x 768) - Click image to enlarge
6. Add some buttons to the ToolStrip control (we'll need 5 buttons).
Resized to 49% (was 1024 x 768) - Click image to enlarge
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:
- <
- FORWARD>>
- REFRESH
- STOP
- HOME
9. Add a ListBox control to your form:
10. Anchor the ListBox control to Top, Right, Bottom:
Resized to 49% (was 1024 x 768) - Click image to enlarge
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):
Resized to 49% (was 1024 x 768) - Click image to enlarge
13. Add 2 buttons at the top of the ListBox (Add and Remove):
Resized to 49% (was 1024 x 768) - Click image to enlarge
So, the form looks like this:
Now, let's pass to the code:
1. Declare the System.Xml namespace:
1 | using System.Xml; |
2. Declare the variable Home as string. It will keep the home page from the XML.
1 | public string Home; |
3. Code for the Form1_Load event:
01 | // Creates a closing event for the form. |
02 | this .Closing += new CancelEventHandler(Form1_Closing); |
03 |
04 | listBox1.MouseDoubleClick += new MouseEventHandler(listBox1_MouseDoubleClick); |
05 | XmlDocument loadDoc = new XmlDocument(); |
06 | loadDoc.Load(Application.StartupPath + "\\load.xml" ); |
07 | Home = loadDoc.SelectSingleNode( "/browser/home" ).Attributes[ "url" ].InnerText; |
08 | webBrowser1.Navigate(Home); |
09 |
10 | foreach (XmlNode favNode in loadDoc.SelectNodes( "/browser/favorites/item" )) |
11 | listBox1.Items.Add(favNode.Attributes[ "url" ].InnerText); |
The XML document is in the following format:
01 |
|
02 |
03 |
|
04 |
05 |
|
06 |
|
07 |
|
08 |
|
09 |
10 |
|
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 "<
1 | private void toolStripButton1_Click( object sender, EventArgs e) |
2 | { |
3 | webBrowser1.GoBack(); |
4 |
5 | } |
5. Code for the "FORWARD>>" button:
1 | private void toolStripButton2_Click( object sender, EventArgs e) |
2 | { |
3 | webBrowser1.GoForward(); |
4 | } |
6. Code for the "REFRESH" button:
1 | private void toolStripButton3_Click( object sender, EventArgs e) |
2 | { |
3 | webBrowser1.Refresh(); |
4 | } |
7. Code for the "STOP" button:
1 | private void toolStripButton4_Click( object sender, EventArgs e) |
2 | { |
3 | webBrowser1.Stop(); |
4 | } |
8. Code for the "HOME" button:
1 | private void toolStripButton5_Click( object sender, EventArgs e) |
2 | { |
3 | webBrowser1.Navigate(Home); |
4 | } |
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):
1 | private void listBox1_MouseDoubleClick( object sender, EventArgs e) |
2 | { |
3 | if (listBox1.SelectedItem != null ) |
4 | webBrowser1.Navigate(listBox1.SelectedItem.ToString()); |
5 | } |
10. The code for the "Add" button:
1 | private void button1_Click( object sender, EventArgs e) |
2 | { |
3 | listBox1.Items.Add(webBrowser1.Url.OriginalString); |
4 | } |
11. The code for the "Remove" button:
1 | private void button2_Click( object sender, EventArgs e) |
2 | { |
3 | // Removes the selected favorites entry. |
4 | listBox1.Items.Remove(listBox1.SelectedItem); |
5 | } |
12. Now we need to program the Closing event of our form:
01 | private void Form1_Closing( object sender, CancelEventArgs e) |
02 | { |
03 | XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + "\\load.xml" , null ); |
04 |
05 | writer.WriteStartElement( "browser" ); |
06 | writer.WriteStartElement( "home" ); |
07 | writer.WriteAttributeString( "url" , "http://www.dreamincode.net" ); |
08 | writer.WriteEndElement(); |
09 | writer.WriteStartElement( "favorites" ); |
10 | for ( int i=0; i < listBox1.Items.Count; i++ ) |
11 | { |
12 | writer.WriteStartElement( "item" ); |
13 | writer.WriteAttributeString( "url" , listBox1.Items[i].ToString()); |
14 | writer.WriteEndElement(); |
15 | } |
16 | writer.WriteEndElement(); |
17 | writer.WriteEndElement(); |
18 | writer.Close(); |
19 | } |
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.
No comments:
Post a Comment