Search code examples
c#browserprogress-bartabcontroltabpage

Progress Bar working on WebBrowser generated in code C#


I'm programming a WebBrowser in C# and I have the following problem: When a button is clicked a new tabPage is generated in tabControl1 with the WebBrowser.

private void button3_Click(object sender, EventArgs e)
    {
        WebBrowser browser = new WebBrowser();
        browser.Dock = DockStyle.Fill;
        browser.Url = new System.Uri("http://google.com");
        tabControl1.TabPages.Add(new TabPage("Aba "+ (tabControl1.TabCount + 1).ToString()));
        tabControl1.TabPages[tabControl1.TabCount - 1].Controls.Add(browser);

    }

I need to know how to make the progress bar work with the webbrowser that belongs to the current active tabPage. How and where do I insert the following code:

toolStripProgressBar1.Maximum = (int) e.MaximumProgress;
toolStripProgressBar1.Value = (int)e.CurrentProgress;

Solution

  • You can use an Anonymous Function:

    WebBrowser browser = new WebBrowser();
    browser.Dock = DockStyle.Fill;
    browser.Url = new System.Uri("http://google.com");
    tabControl1.TabPages.Add(new TabPage("Aba " + (tabControl1.TabCount + 1).ToString()));
    tabControl1.TabPages[tabControl1.TabCount - 1].Controls.Add(browser);
    browser.ProgressChanged += new WebBrowserProgressChangedEventHandler( delegate (object sender, WebBrowserProgressChangedEventArgs events)
        {
            if ((int)events.CurrentProgress > 0)
            {
                toolStripProgressBar1.Maximum = (int)events.MaximumProgress;
                toolStripProgressBar1.Value = (int)events.CurrentProgress;
            }
        });