views:

53

answers:

1

This is in C#

So I'm trying to create an event for ProgressChanged for multiple webBrowser Controls. These are all dynamically created as well as the progress bar. So i can't call upon it prior. What i'm doing is passing the progress bar through object arrays that are ran. It finally gets to the final method in which i create the browser and need to create the Browser.ProgressChanged event.

Here's the code....

private object[] runTests(string banText, object tabControlName, 
        object progressBar, int runThisTest, string testName)
    {
        object[] theReturn = null;
        if (stopTests == false)
        {
            var tabPageBrowser = new TabPage();
            var Browser = new WebBrowser();

            (tabControlName as TabControl).TabPages.Add(tabPageBrowser);
            tabPageBrowser.Name = tabControlName.ToString();
            if (banText == "999999999")
            {
                tabPageBrowser.Text = "History";
            }
            else
            {
                tabPageBrowser.Text = testName;
            }
            tabPageBrowser.Font = new System.Drawing.Font("Trebuchet MS", 8.25F,
                System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            Browser.Dock = DockStyle.Fill;
            Browser.Url = new Uri(testStrings(runThisTest, banText));
            Browser.Name = tabControlName.ToString();
            Browser.ScriptErrorsSuppressed = true;
            tabPageBrowser.Controls.Add(Browser);
            Browser.ProgressChanged += new WebBrowserProgressChangedEventHandler(Browser_ProgressChanged);

            try
            {
                while (Browser.ReadyState != WebBrowserReadyState.Complete)
                {
                    Application.DoEvents();
                }
            }
            catch
            {
                return null;
            }
            IntPtr pHandle = GetCurrentProcess();
            SetProcessWorkingSetSize(pHandle, -1, -1);

            object[] browserObjects = new object[2];
            browserObjects[0] = tabPageBrowser;
            browserObjects[1] = Browser;
            if(browserObjects != null)
            {
                theReturn = browserObjects;
            }
        }
        return theReturn;
    }

Now my question is, how can I add the "progressBar" object when i create the event so i can call upon it when the event has been fired. I'm basically creating a single progress bar for 5+ WebBrowser controls and linking the progress of them together. So for some reason i tried to add the object to the method and it fails out on me. Please assist and Thanks!.

+1  A: 
            Browser.ProgressChanged += Browser_ProgressChanged;
...

        void Browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) {
            if (e.MaximumProgress > 0) {
                int prog = (int)(100 * e.CurrentProgress / e.MaximumProgress);
                progressBar1.Value = prog;
            }
        }

        private ProgressBar progressBar1;
Hans Passant
Theres a problem with that however, I do not know the progressbar Name... It's in an object that i pulled into this method. "object progressBar" ... it's created in an earlier method.
Alex
Just add a field to your class to store the reference. Instead of a local variable.
Hans Passant
Are you saying like a Public object? If thats the case, i didn't think about that.
Alex
It doesn't have to be public. I added it to the code snippet. Update the code that creates the PB to use it.
Hans Passant
Ahh gotcha now! Thanks a lot!
Alex
One other issue i have however is that my numbers aren't right... I have a dynamic number of tests that can be ran... How can i make the progress bar value look accurate... If i do it the way it's listed up there i have a very far off number.. What i tried is this:
Alex
if (currentProgressBar != null) { (currentProgressBar as ToolStripProgressBar).Value += (Int32)(1000 * (100 * e.CurrentProgress / e.MaximumProgress)) / (currentProgressBar as ToolStripProgressBar).Maximum; }
Alex
Start a new question please.
Hans Passant
Ok i created a new question...http://stackoverflow.com/questions/3806762/progress-bar-for-multiple-browsers
Alex