In winForm, how does one implement the Webbrowser control to load html pages ?, Winformwebbrowser
The Webbrowser control is very easy to load html pages. Drag a webbrowser control to the form, and load webpage resources by using the code: webBrowser1.Navigate ("cityApi.html.
To implement winform interaction with Web pages, you need to use the webbrowserappsdocumentcompleted event (that is, InterAction after html documents are loaded ):
The winform background can use the statement webBrowser1.Document. invokeScript ("loadInfo", new object [] {parameter 1 ,..., parameter m}); call the javascript function loadInfo to upload data to the webpage;
Javascript Functions can use the following statement: window. external. ShowMsg (str); call the winform background function ShowMsg (string str );
The following code shows a small example:
[FrmCityApi. cs]
Private void FrmCityApi_Load (object sender, EventArgs e) {webBrowser1.DocumentCompleted + = new System. windows. forms. webBrowserDocumentCompletedEventHandler (this. webBrowser1_DocumentCompleted); // initialize webbrowser to ensure communication between js and C #. webBrowser1.ObjectForScripting = this; // load webbrowser webpage resource webBrowser1.Navigate ("cityApi.html ");} /// <summary> /// load the air index information after the webbrowser file is loaded /// </summary> /// <param name = "sende R "> </param> // <param name =" e "> </param> private void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e) {try {if (webBrowser1.Document! = Null) {// load the air index information webBrowser1.Document. invokeScript ("loadInfo", new object [] {city_name, api_date.ToString (), api. toString (), sitenum. toString ()}) ;}} catch (Exception) {DialogHelper. dlgWarn (this, "loading air index information error ");}}
【CityApi.html]
<Script type = "text/javascript">/* function Run (str) {window. external. showMsg (str);} */function loadInfo (cityName, apiDate, apidata, num) {document. getElementById ("city_name "). innerHTML = cityName; document. getElementById ("api_time "). innerHTML = "Data Update time:" + apiDate; document. getElementById ("city_api_data "). innerHTML = apidata; document. getElementById ("sites_count "). innerHTML = num + "monitoring sites" ;}</script>
End !!!