使用对象创建新事件

发布于 2024-09-24 23:37:25 字数 2232 浏览 2 评论 0原文

这是在 C# 中,

所以我尝试为多个 webBrowser 控件创建 ProgressChanged 事件。这些以及进度条都是动态创建的。所以我不能提前调用它。我正在做的是将进度条传递给运行的对象数组。它最终到达了我创建浏览器并需要创建 Browser.ProgressChanged 事件的最终方法。

这是代码......

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

现在我的问题是,如何在创建事件时添加“progressBar”对象,以便在事件触发时可以调用它。我基本上是为 5 个以上的 WebBrowser 控件创建一个进度条,并将它们的进度链接在一起。因此,出于某种原因,我尝试将对象添加到方法中,但它失败了。请协助并谢谢!

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!.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-10-01 23:37:25
            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;
            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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文