. Net (C #) execute webbrowser. navigate () in the loop statement. This method will continue to be executed after the web page is loaded)

Source: Internet
Author: User

I haven't updated it for a long time. Am I still a programmer?

 

Http://www.cnblogs.com/yangxiaohu1/archive/2009/01/06/1370693.html

 

 

. Net (C #) in the loop statement to execute webbrowser. navigate (); method, each loop waits until the web page is loaded and continues to execute the solution.

Recently, when I was writing a small program, I encountered the following requirements:

If you know a set of web page URLs and want to get the HTML of each web page, you actually want to use the webbrowser in the loop statement to load each web page and then get their HTML,

To implement this function, think about it as a very simple task, but in actual operations there is a problem, because the loading of loop statements and webbrowser is not synchronized, leading to the previous

The previous page has not been loaded, and the next cycle starts again .... the final result is that webbrowser only obtains the HTML of the last page. to solve this problem, what we need to do is

Wait until the web page is loaded, and then execute the next loop to load the following web page ....., according to this idea, I wrote the following program, which is effective after testing.

 

 

Bool
Loading
=
 
True
;
//
This variable indicates whether the webpage is being loaded.



String
Html
=
 
String
. Empty;
Webbrowser Browser
=
 
New
Webbrowser ();


Public
 
Void
Gethtml (
String
[] URLs)
{
Browser. navigated
+ =
 
New
Webbrowsernavigatedeventhandler (browser_navigated );

Foreach
(
String
URL
In
URLs)
{
Loading
=
 
True
;
//
Loading


Browser. navigate (URL );


While
(Loading)
{
Application. doevents ();
//
The next loop is executed only after the loading is completed.


}
}
}


Void
Browser_navigated (
Object
Sender, webbrowsernavigatedeventargs E)
{
Html
=
Browser. documenttext;
//
The obtained HTML.



Loading
=
 
False
;
//
After loading is complete, set this variable to false and the next loop starts to execute.


}

 

 

The above problem is solved, and the following problem occurs: sometimes when a page is loaded, browser_navigated will be executed multiple times.

I checked the online information because the page contains <IFRAME> </iframe>. Each <IFRAME> triggers browser_navigated,

Therefore, the above procedures can be improved as follows:

 

 

Bool
Loading
=
 
True
;
//
This variable indicates whether the webpage is being loaded.



String
Html
=
 
String
. Empty;
Webbrowser Browser
=
 
New
Webbrowser ();


Public
 
Void
Gethtml (
String
[] URLs)
{
Browser. navigated
+ =
 
New
Webbrowsernavigatedeventhandler (browser_navigated );

Foreach
(
String
URL
In
URLs)
{
Loading
=
 
True
;
//
Loading


Browser. navigate (URL );


While
(Loading)
{
Application. doevents ();
//
The next loop is executed only after the loading is completed.


}
}
}


Int
I
=
 
0
;

Void
Browser_navigated (
Object
Sender, webbrowsernavigatedeventargs E)
{
I
++
;

If
(I
%
 
3
 
=
 
0
)
//
Assume that the browser_navigated method is executed three times on each page. This means that all the content of the webpage is loaded)


{
Html
=
Browser. documenttext;
//
The obtained HTML.



Loading
=
 
False
;
//
After loading is complete, set this variable to false and the next loop starts to execute.


}
}

 

 

The above is just a small summary of my work. I have written some notes and hope to help others. I believe there are many ways to solve this problem. I hope you can give me some advice...

 

 

 

 

 

 

C # webbrowser is forcibly opened in this window. It cannot be opened in a new window.

 

 

Sometimes you need to use webbrowser to load URLs to implement some functions. At this time, we do not want to open the link in the page, open in the new window, because in this case, it is actually opened in the default browser of the system, thus breaking away from your webbrowser, you cannot control it.

To solve this problem, you can use the following method:

Assume that the name of webbrowser is webbrowser.

Simple Method-use the load completion event to change the target values of all links and forms to "_ seft ":

View Source
Print
?
private
void
webBrowser_DocumentCompleted(
object
sender, WebBrowserDocumentCompletedEventArgs e)
{
    
// Point the target of all links to this form
    
foreach
(HtmlElement archor
in
this
.webBrowser.Document.Links)
    
{
        
archor.SetAttribute(
"target"
,
"_self"
);
    
}
 
    
// Point the submission targets of all forms to the current form
    
foreach
(HtmlElement form
in
this
.webBrowser.Document.Forms)
    
{
        
form.SetAttribute(
"target"
,
"_self"
);
    
}
}

Cancel New window event

View Source
Print
?
private
void
webBrowser1_NewWindow(
object
sender, CancelEventArgs e)
{
     
e.Cancel =
true
;
}

SetAllowwebbrowserdrop
Set to false (drag and drop prohibited)

SetWebbrowsershortcutsenabledSet to false (do not use shortcut keys)
SetIswebbrowsercontextmenuenabled is set to false(Right-click context menu prohibited)

Address: http://www.cnblogs.com/ATree/archive/2010/09/08/WebBrowser-target_seft_blank.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.