Page 1 of 1

Adding More To Your WebBrowser

#1 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 26 October 2008 - 12:55 PM

In this tutorial I will show you how to Add More To Your WebBrowser.
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
Posted Image

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.
Posted Image

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:
Posted Image

Finished!

Don't Forget I have included the Source Files: Attached File  WebBrowserTutorial_Extended_Part_03_.zip (245.75K)
Number of downloads: 1966

Is This A Good Question/Topic? 0
  • +

Replies To: Adding More To Your WebBrowser

#2 Alan.Warman  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-September 08

Posted 06 December 2008 - 04:04 AM

MOD EDIT: Please don't quote the entire tutorial, especially when you have nothing else to add to the post. Thank You. -b2c
Was This Post Helpful? 0
  • +
  • -

#3 Deathtrh  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 21-December 08

Posted 21 December 2008 - 09:55 PM

Hey could you please tell me how to do this in visual basic 2008 because i have no clue how code in C# :crazy: :crazy: :blink:
Was This Post Helpful? 0
  • +
  • -

#4 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Posted 21 December 2008 - 09:56 PM

Here is a great C# to VB.NET Converter. The best part is it's FREE!

http://labs.develope...harp-to-vb.aspx

This post has been edited by gbertoli3: 21 December 2008 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 Deathtrh  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 21-December 08

Posted 22 December 2008 - 02:36 AM

View Postgbertoli3, on 21 Dec, 2008 - 08:56 PM, said:

Here is a great C# to VB.NET Converter. The best part is it's FREE!

http://labs.develope...harp-to-vb.aspx


Thanks man!!!! :D:D
Was This Post Helpful? 0
  • +
  • -

#6 Guest_nssltd*


Reputation:

Posted 20 February 2010 - 12:49 PM

Hey can i get this in Visual C++ format?
Was This Post Helpful? 0

#7 Guest_JTallis*


Reputation:

Posted 11 December 2010 - 11:16 PM

View Postnssltd, on 20 February 2010 - 11:49 AM, said:

Hey can i get this in Visual C++ format?


It is near enough the same in C++. If you had... "webBrowser.Navigate()" it would be "webBrowser->Navigate()" < Notice how I changed the Period to an Arrow thing. There is a few minor things that need to be changed. I would advise using the MSDN forums, but if you really needed help... feel free to email me at - FullyEncrypted@hotmail.com - Please do not spam, and please do not expect I'll know all the answers to your questions. :)
Was This Post Helpful? 0

#8 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Posted 21 April 2013 - 07:08 PM

I'm getting an error tat it can't find the xml file



public void Bookmarks_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
loadDoc.Load(Application.StartupPath + "\\Bookmarks.xml");

foreach (System.Xml.XmlNode favNode in loadDoc.SelectNodes("/Bookmarks/Item"))
{
listView1.Items.Add(favNode.Attributes["url"].InnerText);
}

it says file not found exception not handled
Was This Post Helpful? 0
  • +
  • -

#9 Parabellum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-April 13

Posted 23 April 2013 - 06:21 AM

fixed my xml problem,
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9381
  • View blog
  • Posts: 35,227
  • Joined: 12-June 08

Posted 23 April 2013 - 06:59 AM

What was the error? What was the solution?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1