1, Main uses: Enables the user to navigate the webpage in the form.
2. Note: The WebBrowser control consumes a large amount of resources. Be sure to call the Dispose method when you are finished using the control to ensure that all resources are released in a timely manner. The Dispose method must be called on the same thread that attaches the event, and the line thread always be a message or user interface (UI) thread.
3. WebBrowser use the following members to navigate a control to a specific URL, to move backwards and forwards in the navigation history list, and to load the current user's home page and search page:
1.URL property: Readable, writable, to get or set the URL of the current document.
The WebBrowser control maintains a list of the history of all pages accessed during a browsing session. When you set the URL property, the WebBrowser control navigates to the specified URL and adds the URL to the end of the history list.
The WebBrowser control stores the Web page of the most recently visited site in the cache of the local hard disk. Each page can specify an expiration date that indicates when the page is retained in the cache. When a control navigates to a page, if the page has a cached version, it saves time by displaying the contents of the cache directly without having to download the page again. Use the Refresh method to force the WebBrowser control to reload the current page by downloading, ensuring that the control displays the latest version.
Note: This property also contains the URL of the current document, even if another document has been requested. If you set the value of this property and then immediately retrieve the value again, the retrieved value may be different than the value you set if the WebBrowser control has not had time to load the new document.
2.Navigate method: Loads the document at the specified location into the WebBrowser control.
3.GoBack method: If the previous page in the navigation history is available, navigate to the page with the WebBrowser control.
Returns true if the navigation is successful, or false if the previous page in the navigation history is not available.
The WebBrowser control maintains a list of the history of all pages accessed during a browsing session. You can use the GoForward method to implement a Back button.
Use the CanGoBack property to determine whether navigation history is available and contains the previous page. Handles the Cangobackchanged event to receive notification when the CanGoBack property value changes.
4.GoForward method: If the next page in navigation history is available, navigate to the page with the WebBrowser control.
Returns true if the navigation is successful, or false if the next page in navigation history is not available.
The WebBrowser control maintains a list of the history of all pages accessed during a browsing session. You can use the GoForward method to implement a "forward" button.
Use the CanGoForward property to determine whether navigation history is available and whether to include pages after the current page. Handling Cangoforwardchanged events, receiving notifications when CanGoForward property values change
5.GoHome method: Navigates the WebBrowser control to the home page of the current user.
6.GoSearch method: Navigates the WebBrowser control to the current user's default search page.
The default search page is stored under the HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Search page registry key in the registry.
To use a different search page instead of the default search page, call the Navigate method or specify the Url property.
7.Refresh method: Reloads the document currently displayed in the WebBrowser control.
8.Stop method: Cancels all pending navigation and stops all dynamic page elements (such as background sounds and animations).
If the navigation is unsuccessful, a page indicating the problem appears. Using either of these members to navigate causes Navigating, navigated, and documentcompleted events to occur at different stages of the navigation.
4. ObjectForScripting property: Gets or sets an object that can be accessed by scripting code that is contained in the Web page that is displayed in the WebBrowser control.
Use this property to enable the communication between the Web page hosted by the WebBrowser control and the application that contains the WebBrowser control. Use this property to integrate dynamic HTML (DHTML) code with the client application code. The object specified for this property can be used as a window.external object (the built-in DOM object used for host access) for Web page scripting.
You can set this property to any COM-visible object whose public properties and methods you want to use for scripting code. You can mark a class by using ComVisibleAttribute to make it a COM-visible class.
To call a function defined in a Web page from the client application code, use the Htmldocument.invokescript method of the HTMLDocument object that can be retrieved from the Document property.
5. AllowNavigation property: Gets or sets a value that indicates whether the control can navigate to another page after it loads its initial page.
6. Allowwebbrowserdrop property: Gets or sets a value that indicates whether the WebBrowser control navigates to the document dragged onto it.
7, webbrowsershortcutsenabled property: Whether to enable WebBrowser with the shortcut keys.
8. scripterrorssuppressed property: Gets or sets a value that indicates whether the WebBrowser control displays an error dialog box when a script error occurs.
9, iswebbrowsercontextmenuenabled Property: Whether the right-click menu enabled.
Source: MSDN Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser (v=vs.80). aspx
Summary of practical methods of C#winform WebBrowser (II.)
Practical Method 1: Get status bar information
void Webbrowser1_statustextchanged (object sender, EventArgs e)
{
Label1. Text = Webbrowser1.statustext;
}
Practical Method 2: Change the Address bar address after page jump
It is most appropriate to change the address bar address in the navigated event handler:
private void Webbrowser1_navigated (object sender, WebBrowserNavigatedEventArgs e)
{
TextBox1.Text = WebBrowser1.Url.ToString ();
}
Practical Method 3: Set up a radio box
It is recommended that you set up a radio box by using the Click event instead of modifying the property:
WebBrowser1.Document.GetElementById ("Rbt_a"). InvokeMember ("click");
Practical Method 4: Set the linkage type Drop-down list
The more common linkage-type multi-level Drop-down list is the province/city choice, this case directly set the properties of the selection will not trigger linkage, the need to execute the final trigger event function to work correctly:
foreach (HtmlElement f in s.getelementsbytagname ("option"))
{
if (F.innertext = = "Beijing")
{
F.setattribute ("Selected", "Selected");
}
Else
{
F.setattribute ("Selected", "");
}
}
S.raiseevent ("onchange");
All four of the above methods are referred to: http://www.cnblogs.com/SkyD/archive/2009/04/23/1441696.html
Practical method One: the corresponding Web event in WinForm
Suppose the HTML source code is as follows:
<body>
<input type= "button" id= "Btnclose" value= "Off"/>
</body>
HTMLDocument htmldoc = webbrowser.document;
HtmlElement btnelement = htmldoc.all["Btnclose"];
if (btnelement!= null)
{
Btnelement.click + = new Htmlelementeventhandler (Htmlbtnclose_click);
}
It's very simple. So a little more advanced--we all know that an HTML element may have many kinds of events, and HtmlElement This class gives only the most common and common ones. So, how do you respond to other events? This is also very simple, only need to call the HtmlElement Attacheventhandler on it:
Btnelement.attacheventhandler ("onclick", New EventHandler (Htmlbtnclose_click));
This sentence is equivalent to the above Btnelement.click + = new Htmlelementeventhandler (Htmlbtnclose_click);
For other events, it is OK to replace "onclick" with the name of the event. For example:
Formelement.attacheventhandler ("onsubmit", New EventHandler (Htmlform_submit));
Practical method Two: The simulation form automatically fills in and submits
Assume that there is a simple login page, enter the username password, click the "Login" button to log in. The ID of the User name input box (or name, below) is username, the ID of the Password input box is password, and the ID of the "Login" button is Submitbutton, So we just need to use the following code in the WebBrowser documentcompleted event:
HtmlElement btnsubmit = webbrowser.document.all["Submitbutton"];
HtmlElement Tbuserid = webbrowser.document.all["username"];
HtmlElement tbpasswd = webbrowser.document.all["password"];
if (Tbuserid = null | | tbpasswd = NULL | | | btnsubmit = NULL)
Return
Tbuserid.setattribute ("Value", "Smalldust");
Tbpasswd.setattribute ("Value", "12345678");
Btnsubmit.invokemember ("click");
There is another way to get a form element instead of a button and a Submit method of a FORM element:
HtmlElement formlogin = webbrowser.document.forms["LoginForm"];
......
Formlogin.invokemember ("submit");
This method is not recommended because many Web pages now add onclick events to the submit button to make the most basic validation of the submitted content. If you use the form's Submit method directly, the validation code will not be executed and may cause errors.
Practical method Three: calling a script
The first is the function that is already defined in the script that invokes the Web page. Suppose the following JavaScript is in HTML:
function Doadd (A, b) {
return a + B;
}
So, we're going to call it in WinForm, just the following code:
Object osum = WebBrowser.Document.InvokeScript ("Doadd", new object[] {1, 2});
int sum = Convert.ToInt32 (osum);
Second, if we want to execute a script that is not already in a Web page, what do we do? This. NET's class is not provided, it seems to rely on COM. IHTMLWindow2 can execute arbitrary strings as script code.
String scriptline01 = @ "function Showpageinfo () {";
String scriptline02 = @ "var numlinks = document.links.length; ";
String scriptline03 = @ "var numforms = document.forms.length; ";
String scriptline04 = @ "var numimages = document.images.length; ";
String scriptline05 = @ "var numscripts = document.scripts.length; ";
String scriptline06 = @ alert (' statistic result: \ r \ n Link number: ' + numlinks + ";
String scriptline07 = @ "' \ r \ n Table singular: ' + numforms +";
String scriptline08 = @ "' \ r \ n Image number: ' + numimages +";
String scriptline09 = @ "' \ r \ n Script number: ' + numscripts";};
String scriptline10 = @ "Showpageinfo ();";
String strscript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +
Scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;
IHTMLWindow2 win = (IHTMLWindow2) WebBrowser.Document.Window.DomWindow;
Win.execscript (Strscript, "Javascript");
The above three practical methods turn to: http://www.cnblogs.com/smalldust/archive/2006/03/08/345561.html
Finally: Call in the script