如何使WebBrowser等待,直到它完全加载? [英] How to make WebBrowser wait till it loads fully?

查看:896
本文介绍了如何使WebBrowser等待,直到它完全加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有上有一个Web浏览器控件一个C#的形式。



我想在一个循环来访问不同的网站。



不过,我无法控制的URL地址加载到我的表格的网页浏览器元素。



这是我使用过的URL地址导航功能:

 公共字符串WebNavigateBrowser(字符串urlString,web浏览器WB)
{
字符串数据=;
wb.Navigate(urlString);
,而(!wb.ReadyState = WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
数据= wb.DocumentText;
返回数据;
}



我怎样才能让我的循环等待,直到它完全加载?



我的循环是这样的:

 的foreach(URL中的字符串urlAddresses)
{
WebNavigateBrowser(urlAddresses,webBrowser1);
//我需要添加一个代码,使表格网页浏览器要等到它加载
}


< DIV CLASS =h2_lin>解决方案

这添加到您的代码:

  webBrowser1.DocumentCompleted + =新WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 



填写此功能



 私人无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E){
//这行,所以你只能做事件一次
如果(e.Url!= webBrowser1.Url)
返回;


//你实际的代码



}


I have a C# form with a web browser control on it.

I am trying to visit different websites in a loop.

However, I can not control URL address to load into my form web browser element.

This is the function I am using for navigating through URL addresses:

public String WebNavigateBrowser(String urlString, WebBrowser wb)
{
    string data = "";
    wb.Navigate(urlString);
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    data = wb.DocumentText;
    return data;
}

How can I make my loop wait until it fully loads?

My loop is something like this:

foreach (string urlAddresses in urls)
{
    WebNavigateBrowser(urlAddresses, webBrowser1);
    // I need to add a code to make webbrowser in Form to wait till it loads
}

解决方案

Add This to your code:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Fill in this function

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
     //This line is so you only do the event once   
     if (e.Url != webBrowser1.Url) 
        return;


        //do you actual code        



    }

这篇关于如何使WebBrowser等待,直到它完全加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆