Question

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;
Was it helpful?

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;
        }
    });

OTHER TIPS

I had a problem with crushing cause the progressbar was all ready full, so here's this problem solved.

    private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        if ((int)e.CurrentProgress > 0)
        {
            ProgressBar.Maximum = (int)e.MaximumProgress;
            if (ProgressBar.Maximum == (int)e.MaximumProgress)
                ProgressBar.Value = 0;
            ProgressBar.Value = (int)e.CurrentProgress;                
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top