Today, we will use winform as a web browser. If we open only one window, the code is very simple. If we open anything like Baidu or 163, we don't need to write code, just set it in the URL. However, clicking the link in the window will jump to IE to open it, and it is unfriendly. After referring to the Code on the internet, I changed my own code and opened it internally.
The Code is as follows:
Private void webbrowser1_newwindow (Object sender, canceleventargs E)
{
This. webbrowser1.scripterrorssuppressed = true; // The Script Error is disabled.
E. Cancel = true; // disable opening in a new window
Webbrowser1.navigate (webbrowser1.statustext); // jump to the new webpage
}
These few simple codes can be opened inside the vast majority of web pages, but a few of them cannot be opened due to the special nature of the Code. We will perform the following processing:
Let's first write a method:
Private void setallwebitemself (htmlelementcollection items)
{
Try
{
Foreach (htmlelement item in items)
{
If (item. tagname. tolower (). Equals ("iframe", stringcomparison. ordinalignorecase) = false)
{
Try
{
Item. setattribute ("target", "_ Self ");
}
Catch
{}
}
Else
{
Try
{
Htmlelementcollection fitems = item. Document. Window. Frames [item. Name]. Document. All;
This. setallwebitemself (fitems );
}
Catch
{}
}
}
}
Catch
{
}
}
The above method can be called in documentcompleted to solve the above problem:
Code:
Private void webbrowserappsdocumentcompleted (Object sender, webbrowserdocumentcompletedeventargs E)
{
This. webbrowser1.scripterrorssuppressed = false;
This. setallwebitemself (this. webbrowser1.document. All );
}