This paper summarizes the common practical skills of WinForm WebBrowser in C #, and has a good reference value for C # program development. Described separately as follows:
Method 1: Get status bar information
void Webbrowser1_statustextchanged (object sender, EventArgs e) { Label1. Text = Webbrowser1.statustext;}
Method 2: Change the Address bar address after the page jumps
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 ();}
Method 3: Set the Radio box
Instead of modifying the properties: WebBrowser1.Document.GetElementById ("Rbt_a"), we recommend that you use the Click event to set a radio box. InvokeMember ("click");
Method 4: Set the Linkage drop-down list
The more common multi-level drop-down list is the province/city selection, in which case the properties of the directly set selection will not trigger linkage, and need to be executed at the end of the trigger event function to work properly: foreach (HtmlElement f in S. getElementsByTagName ("option")) { if (F.innertext = = "Beijing") { f.setattribute ("Selected", "selected"); } Else { F.setattribute ("Selected", "");} } S.raiseevent ("onchange");
Method 5: Respond to Web events in WinForm
Suppose the HTML source code is as follows:
HTMLDocument htmldoc = webbrowser.document; HtmlElement btnelement = htmldoc.all["Btnclose"];if (btnelement! = null) { Btnelement.click + = new Htmlelementeventhandler (Htmlbtnclose_click);}
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 EventHandler (Htmlbtnclose_click));
This sentence is equivalent to the above Btnelement.click + = new Htmlelementeventhandler (Htmlbtnclose_click);
For other events, change the "onclick" to the name of the event. For example:
Formelement.attacheventhandler ("onsubmit", New EventHandler (Htmlform_submit));
Method 6: Simulate form auto-fill and submit
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) C0/>return;tbuserid.setattribute ("Value", "Smalldust"); Tbpasswd.setattribute ("Value", "12345678"); Btnsubmit.invokemember ("click");
There is really another way to get a form element, rather than a button, and to 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.
Method 7: Call the script
The first is a function that has already been defined in the script that invokes the Web page. Suppose the following JavaScript is in the 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);
Secondly, what if we want to execute a script that is not originally in a Web page? This. NET class does not provide, 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 (' Statistical result of the webpage: \ r \ n Link number: ' + numlinks + '; string scriptline07 = @ " ' \ r \ n Table singular: ' + numforms + "; string scriptline08 = @" ' \ r \ n Image number: ' + Numimages + "; string scriptline09 = @" ' \ r \ n Script: ' + numscripts ';} "; String scriptline10 = @ "Showpageinfo ();"; String strscript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 + scriptline06 + Scriptlin E07 + scriptline08 + scriptline09 + scriptline10;ihtmlwindow2 win = (IHTMLWindow2) WebBrowser.Document.Window.DomWindow; Win.execscript (Strscript, "Javascript");
Finally: Is it possible to invoke the code in WinForm in the script? Oh, of course it is possible.
The following code example demonstrates how to use the ObjectForScripting property. In this example, the ObjectForScripting property is set to the current form.
Using system;using system.windows.forms;using System.Security.Permissions; [PermissionSet (SecurityAction.Demand, name= "FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute (True)]public class form1:form{private WebBrowser WebBrowser1 = New WebBrowser (); Private button button1 = New button (); [STAThread] public static void Main () {application.enablevisualstyles (); Application.Run (New Form1 ()); } public Form1 () {button1. Text = "Call script code from client code"; Button1. Dock = Dockstyle.top; Button1. Click + = new EventHandler (button1_click); Webbrowser1.dock = DockStyle.Fill; Controls.Add (WebBrowser1); Controls.Add (button1); Load + = new EventHandler (Form1_Load); } private void Form1_Load (object sender, EventArgs e) {webbrowser1.allowwebbrowserdrop = false; webbrowser1.iswebbrowsercontextmenuenabled = false; webbrowser1.webbrowsershortcutsenabled = false; Webbrowser1.objectforscripting = this; Uncomment the following LINE when is finished debugging. Webbrowser1.scripterrorssuppressed = true; Webbrowser1.documenttext = "It is believed that the example mentioned in this paper has some reference value to the C # program design of everyone.
In addition to the Declaration,Running GuestArticles are original, reproduced please link to the form of the address of this article
A summary of WinForm WebBrowser practical tips for C #
This address: http://www.paobuke.com/develop/c-develop/pbk23591.html
Related content summary of various timer usages in C # C # parameters follow ASCII code from small to Large (dictionary order) C # Web application debugging Open external Access steps parsing C # Learning notes Collation _ variables and other basic grammar (must SEE)
C # Get client-related Information instance summary C # methods for DataGridView Handling unbound Columns C # implementation method of getting disk space size C # implements the method of automatically creating a Word document from a templateA summary of WinForm WebBrowser practical tips for C #