If you have not read my tutorial on Creating a WebBrowser, click here.
If you have not read my tutorial on Extending the WebBrowser, click here.
We will be Adding Favorites and Customizing our WebBrowser.
Favorites
Create a new Form. Set the Form's Size to 500 by 320. Set the Minimum Size to 500 by 320. Set the Maximum Size to 500 by 320. Set the MaximizeBox property to False.
Add a Toolstrip. Add 2 Buttons and 1 TextBox to the ToolStrip. Set the TextBox size to 290 by 25. Set the Text of the First Button to Add Favorite and the Second Button to Remove Favorite. Name the First button addButton, the Second button removeButton, and the TextBox urlTxt.
Now add a ListView to the Form. Make sure that your ListView's View is set to List. Also set the MultiSelect to False. Set it's Dock Property to Fill, it's GridLines to True, and it's FullRowSelect to True.
Make sure you set all of your control's Modifier Property to Public
This is what your Favorites Window should look like

Now for the code
Go to your addButton's Click() event and type:
ListViewItem item = new ListViewItem(urlTxt.Text); listView1.Items.Add(urlTxt.Text);
Go to your removeButton's Click() event and type:
try
{
listView1.Items.RemoveAt(listView1.SelectedIndices[0]);
}
catch
{
MessageBox.Show("You need to select an item");
}
Go to your Favorites Form's Load() event and type:
System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
loadDoc.Load(Application.StartupPath + "\\Favorites.xml");
foreach (System.Xml.XmlNode favNode in loadDoc.SelectNodes("/Favorites/Item"))
{
listView1.Items.Add(favNode.Attributes["url"].InnerText);
}
Now go to your Favorites Form's Closing() event and type:
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Application.StartupPath + "\\Favorites.xml", null);
writer.WriteStartElement("Favorites");
for (int i = 0; i < listView1.Items.Count; i++)
{
writer.WriteStartElement("Item");
writer.WriteAttributeString("url", listView1.Items[i].Text);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Close();
Now go to your Main Form and add a Sub Item Called FavoritesItem, place it under the View menu. Go to your FavoritesItem's Click() event and type
Favorites fav = new Favorites(); fav.urlTxt.Text = webBrowser.Url.ToString(); fav.StartPosition = FormStartPosition.CenterParent; fav.ShowDialog(this);
Customizing
Go to your Main Form and drag a StatusStrip onto your form.

Next add a Label and a ProgressBar to the StatusStrip. Name the Label statusLabel and the ProgressBar statusProgress. Once you have done that go to your Form's Load() event and type this.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser.GoHome();
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
webBrowser.Navigating += new WebBrowserNavigatingEventHandler(webBrowser_Navigating);
webBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser_ProgressChanged);
webBrowser.StatusTextChanged += new EventHandler(webBrowser_StatusTextChanged);
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
statusProgress.Visible = false;
url.Text = e.Url.ToString();
Text = webBrowser.document.Title + " - Web Browser Tutorial";
}
private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
statusProgress.Visible = true;
}
private void webBrowser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
statusProgress.Value = (int)e.CurrentProgress;
}
private void webBrowser_StatusTextChanged(object sender, EventArgs e)
{
statusLabel.Text = webBrowser.StatusText;
}
Here is what your Main Form should look like:

Finished!
Don't Forget I have included the Source Files:
WebBrowserTutorial_Extended_Part_03_.zip (245.75K)
Number of downloads: 1703





MultiQuote








|