Set the value of a control
/// <summary> ///double judgment based on id,name and set values/// </summary> /// <param name= "TagName" ></param> /// <param name= "id" ></param> /// <param name= "value" ></param> Private voidSettxt (stringTagName,stringNamestringIdstringvalue) {HTMLDocument docx= <strong><font color="#FF0000">webBrowser</font></strong>1. Document; foreach(HtmlElement iteminchdocx. getElementsByTagName (TagName)) {if(item. GetAttribute ("name") !=NULL&& item. GetAttribute ("name") = = Name | | Item. Id = =ID) {Try{item. Focus (); Item. SetAttribute ("value", value); } Catch{item. SetAttribute ("value", value); } } } }
Returns the contents of the picture </IMG> in the specified webbrowser to be referenced mshtml
/// <summary>///returns the picture in the specified WebBrowser</IMG>the contents of the diagram in/// </summary>/// <param name= "Webctl" >WebBrowser Controls</param>/// <param name= "Imgetag" >img Element</param>/// <returns>img Object</returns>PrivateImage getwebimage (WebBrowser webctl, HtmlElement imgetag) {HTMLDocument doc=(HTMLDocument) WebCtl.Document.DomDocument; HTMLBody Body=(HTMLBody) doc.body; Ihtmlcontrolrange rang=(Ihtmlcontrolrange) body.createcontrolrange (); IHTMLControlElement IMG= (ihtmlcontrolelement) imgetag.domelement;//Image AddressImage oldimage =Clipboard.getimage (); Rang.add (IMG); Rang.execcommand ("Copy",false,NULL);//Copy to MemoryImage Numimage =Clipboard.getimage (); Try{clipboard.setimage (oldimage); } Catch { } returnnumimage;}
A general-purpose WebBrowser class that encapsulates common methods
Public Partial classHtmlElement {//get elements by name PublicHtmlElement Getelement_name (WebBrowser WB,stringName) {HtmlElement e=WB. Document.all[name]; returne; } //get elements by ID PublicHtmlElement getelement_id (WebBrowser WB,stringID) {htmlelement e=WB. document.getElementById (ID); returne; } //get elements based on index PublicHtmlElement Getelement_index (WebBrowser WB,intindex) {HtmlElement e=WB. Document.all[index]; returne; } //The type gets the meta and uses it without the name and ID PublicHtmlElement Getelement_type (WebBrowser WB,stringtype) {HtmlElement e=NULL; HtmlElementCollection Elements= WB. document.getElementsByTagName ("input"); foreach(htmlelement elementinchelements) { if(element. GetAttribute ("type") ==type) {e=element; } } returne; } //The type gets the meta, is used without name and ID, and specifies the first of the homogeneous type, Getelement_type () upgrade version PublicHtmlElement Getelement_type_no (WebBrowser WB,stringTypeinti) {intj =1; HtmlElement e=NULL; HtmlElementCollection Elements= WB. document.getElementsByTagName ("input"); foreach(htmlelement elementinchelements) { if(element. GetAttribute ("type") ==type) { if(J = =i) {e=element; } J++; } } returne; } //get form table sole name name, return form PublicHtmlElement Getelement_form (WebBrowser WB,stringform_name) {HtmlElement e=WB. Document.forms[form_name]; returne; } //set the value of the element Value property Public voidWrite_value (HtmlElement E,stringvalue) {E.setattribute ("value", value); } //methods of executing elements, such as: Click,submit (requires form table sole name), etc. Public voidBtn_click (HtmlElement E,strings) {e.invokemember (s); } }
Multi-threaded execution of WebBrowser methods:
WebBrowser only supports STA mode
Private voidThreadwebbrowser (stringURL) {Thread tread=NewThread (NewParameterizedthreadstart (Begincatch)); Tread. Setapartmentstate (ApartmentState.STA);//note the thread unit that is createdtread. Start (URL);} Private voidBegincatch (Objectobj) { stringURL =obj. ToString (); WebBrowser WB=NewWebBrowser (); Wb. Scripterrorssuppressed=true; //Here navigate a blank page must beWb. Navigate ("About:blank"); stringHtmlcode ="";//this assigns the HTML string dataWB. document.write (Htmlcode); //perform other actions}
C # Webbrowser Common methods and multi-threaded calls