C # winform webbrowser allows the webpage to be opened inside

Source: Internet
Author: User

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

 

 

 

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.