Say with the WebBrowser class, finally do not have to manually encapsulate the SHDOCVW axwebbrowser this ActiveX control. This class if only as a and IE browser, it is too boring (or rather than directly with IE). So, whether we want to do a "custom version of IE", or want to use HTML to do the user interface (refers to WinApp rather than webapp. Many stand-alone software, including the Windows Help Support Center, is HTML-enabled, all without the interaction of Windows Form and the Web pages contained in WebBrowser. This article will take a few practical examples to illustrate the interaction between the Web pages contained in WinForm and WebBrowser.
The following code assumes that you have created a Windows Form with a webBrowser named "WebBrowser" on it.
Study Case 1: Responding to Web page events with WinForm event handler
Now there is a Windows application, which has only one WebBrowser on the interface, displaying a local HTML file as the interface. The problem now is that all logic can be placed in the HTML file, but the "close" button is difficult-usually, there is no way for the Web page to control the browser directly, let alone end the WinForm program.
However, in. Net 2.0, "events that respond to Web pages by Windows Form" have become a reality.
In. NET 2.0, the entire HTML document and the individual HTML elements it contains correspond to. NET objects such as HTMLDocument, HtmlElement, and so on. So just find the HtmlElement object for the Close button and add event handler for its Click event.
Suppose the HTML source code is as follows:
"button" id="btnclose" value=" off " /></body>
Then find the button and add the event handler code as follows:
HTMLDocument Htmldoc == htmldoc.all["btnclose"]; if NULL {new Htmlelementeventhandler (Htmlbtnclose_click);}
Where Htmlbtnclose_click is the event Handler when the Web button is pressed.
Very simple, huh? A little bit more advanced-we all know that an HTML element can have a lot of different events, whereas the HtmlElement 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 HtmlElement Attacheventhandler can be:
Btnelement.attacheventhandler ("onclick"new//
For other events, change the "onclick" to the name of the event. For example:
Formelement.attacheventhandler ("onsubmit"new EventHandler (htmlform_ Submit));
Study Case 2: Automatic filling and submission of forms (form)
It is not difficult for our WebBrowser to have automatic fill-in and even auto-submit functions.
Let's say you have a simple login page, enter your username and password, and sign in by clicking the Sign In button. The ID of the known User name input box (or name, below) is username, the ID of the Password input box is password, the ID of the "Login" button is Submitbutton, Then 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");
Here we use setattribute to set the "value" property of the text box, and use InvokeMember to invoke the button's "click" Method. Because different HTML elements have various properties and methods,. Net 2.0 provides a unified htmlelement to summarize the various HTML elements while providing both methods to invoke element-specific functionality.
※ There is another way to submit a form is to get the form element instead of the button and use the Submit method of the form element:
HtmlElement formlogin = webbrowser.document.forms["loginform"]; // ... Formlogin.invokemember ("submit");
This article is not recommended this way, because the current page, a lot of the submit button to add the OnClick event, to the content of the submission to do the most basic validation. If you use the Submit method of the form directly, these validation codes will not be executed and may cause errors.
Study Case 3: Find and select text
This time we want to implement a search function that is exactly the same as IE to find the text within the Web page.
The FindText method for text lookup to be aided by the TextRange object. But. NET does not have this object. This is because the Htmldocument,htmlwindow,htmlelement class, provided by. Net 2.0, is simply an incomplete encapsulation of the COM component of the original MSHTML, providing only part of the functionality of the MSHTML. So many times, we still have to use MSHTML to achieve the functions we need. Fortunately, these. NET classes provide DOMDocument this property, making it easy to put. NET object for use with COM objects. The following code shows how to find the text of a Web page. (You need to add a reference to the MSHTML, plus the using mshtml;)
Public Partial classsearchdemo:form{//Create a Lookup TextRange (IHTMLTxtRange interface) PrivateIHTMLTxtRange Searchrange =NULL; PublicSearchdemo () {InitializeComponent (); } Private voidbtnSearch_Click (Objectsender, EventArgs e) { //The DOMDocument property of document is the COM object inside the object. IHTMLDocument2 document =(IHTMLDocument2) webBrowser.Document.DomDocument; stringKeyword =TxtKeyword.Text.Trim (); if(keyword = ="") return; //the search logic for IE is that if you have a selection, you start with the beginning of the current selection and 1 characters, and if not, look for it from the beginning of the page. //This logic is actually a bit inappropriate, we do not have to tube here, and ie consistent. if(Document.selection.type.ToLower ()! ="None") {Searchrange=(IHTMLTxtRange) document.selection.createRange (); Searchrange.collapse (true); Searchrange.movestart ("character",1); } Else{ihtmlbodyelement Body=(ihtmlbodyelement) document.body; Searchrange=(IHTMLTxtRange) body.createtextrange (); } //if found, select (highlight) the keyword; otherwise pop up the message. if(Searchrange.findtext (keyword,1,0) {searchrange.Select(); } Else{MessageBox.Show ("the end of the document has been searched. "); } }}
Http://www.sosuo8.com/article-2011/webbrowser-control-form-automatically-fill-and-submit.htm