Use the webbrowser control in. Net to implement winform and webform interaction 1

Source: Internet
Author: User
The webbrowser control in. Net allows you to load windows form applications Program Web pages and other files using browsers. You can use the webbrowser control to add existing Web framework control items to Windows form. Customer End application.
Or directly view Code Right.
The attributes, methods, and events provided by the webbrowser Control item can be used to implement the control items of Internet Explorer.
Webbrowser1.navigate (" Www.itwis.com "); // Load the file at the specified position to webbrowser
Webbrowser1.goback (); // Previous Page
Webbrowser1.goforward (); // next page
Webbrowser1.refresh (); // refresh
Webbrowser1.gohome (); // Home Page
Common webbrowser methods are provided here,
The above code loads the home page of our garden into the webbrowser control. If you want to generate your own webpage content in an application, you can set the documenttext attribute. You can also use the document attribute to process the current webpage content. The following code uses the documenttext attribute to display the webpage content. Use the document attribute to process the displayed webpage.
1 private void btndocumenttext_click (Object sender, eventargs E)
2 {
3 string szhtml = @"
4 <HTML>
5 6 <title> documenttext </title>
7 8
9 <body>
10 Please enter your name: <br/>
11 <input type = 'text' name = 'name'/> <br/>
12 <a href = 'HTTP: // www.microsoft.com '> send input to method of Form class </a>
13
14 </body>
15 17 webbrowser1.documenttext = szhtml; 18
19}
20
21 private void webbrowser1_navigating (Object sender, webbrowsernavigatingeventargs E)
22 {
23 system. Windows. Forms. htmldocument document = This. webbrowser1.document;
24
25 if (document! = NULL & document. All ["name"]! = NULL & string. isnullorempty (document. All ["name"]. getattribute ("value ")))
26 {
27 E. Cancel = true;
28 system. Windows. Forms. MessageBox. Show ("You must enter your name before you can navigate to" + E. url. tostring ());
29}
30
31}
Since we can generate our own Webpage Through documenttext, can we operate this webpage like using IE ?, The answer is yes. You can operate the webbrowser control items just like you do with web programs. For example, we can add scripts and CSS. Of course, if you are familiar with the HTML Object Model (DOM), you can also use the document attribute to process the current web page content. The following example adds a Javascript script to control the webpage. If you want to write a large number of javascriot code in the winfrom program, and the code will eventually be converted to the string type and loaded to the webbrowser, it will be very painful, but it doesn't matter, we can create a JS file and put it into the resource. You only need to load it from the resource when using it. Here I create a file named clientscript. js.
1 <script language = "JavaScript">
2 function clickevent (name)
3 {
4 alert ("Hello:" + name );
5}
6
7 function keydown ()
8 {
9 If (event. keycode = 116)
10 {
11 event. keycode = 0;
12 event. returnvalue = false;
13}
14
15 return false;
16}
String szclientscript = managedwebbrowser. properties. Resources. ResourceManager. getstring ("clientscript ");
String szwebbrowsertext = "<HTML>" +
"<Head>" +
"<Title> </title>" +
Szclientscript +
"</Head>" +
"<Body onkeydown = \" keydown () \ "oncontextmenu = \" event. returnvalue = false \ ">" +

"Please enter your name: <br/>" +
"<Input type = 'text' name = 'name'/> <br/>" +
"<Font onclick = 'clickevent (name. Value) '> click here </font>" +
"</Body> Webbrowser1.documenttext = szwebbrowsertext;
Webbrowser is the control item under system. Windows. forms, that is, it means that it is used in the wimform program, so how can the web page loaded by webwrower be processed under the winform program. For example, in the preceding example, "<font onclick = 'clickevent (name. Value) '> click here </font> ". Here, the click event is processed by a script. How can we make this click event be processed in winform? Make some changes here. To access the client application from the script code, you must set the objectforscripting attribute. The instruction code can be used to access the specified object as a window. External Object.

Use the objectforscripting attribute to enable communication between web pages loaded by the webbrowser Control item and applications containing the webbrowser Control item.
This attribute allows you to integrate dynamic hypertext markup language (DHTML) code with the user-side application code.
The object specified for this attribute can use the web script code as a window. External object, which is a built-in DOM object provided to access the main application.
1 private void btnscriptevent_click (Object sender, eventargs E)
2 {
3
4 // This is the handler for loading the script into the web browser control and allowing us to interact
5 // between the script in the browser control and this form class
8 // set the objectforscripting property of the Web browser control to point to this form class
9 // This will allow us to interact with methods in this form class via the window. External Property
10 webbrowser1.objectforscripting = this;
11
12 string szwebbrowsertext = "<HTML>" +
13 "14 "<title> </title>" +
15 "16 "<body onkeydown = \" keydown () \ "oncontextmenu = \" event. returnvalue = false \ ">" +
17
18 "Please enter your name: <br/>" +
19 "<input type = 'text' name = 'name'/> <br/>" +
20 "<font onclick = 'window. External. clickevent (name. Value) '> click here </font>" +
21 "</body> 22
23
24 webbrowser1.documenttext = szwebbrowsertext;
25}
26 Public void clickevent (string username)
27 {
28 // simply echo out the name that the user typed in the input box of the HTML page
29 If (system. Threading. thread. currentthread. currentuiculture. textinfo. isrighttoleft = true)
30 MessageBox. Show ("hello" + username, "Managed Web browser sample", messageboxbuttons. OK, messageboxicon. Information, messages. button1, messageboxoptions. rightalign | messageboxoptions. rtlreading );
31 else
32 MessageBox. Show ("hello" + username, "Managed Web browser sample", messageboxbuttons. OK, messageboxicon. Information, messageboxdefaultbutton. button1 );
33
34}
The objectforscripting attribute is set to this. Note: This [comvisible (true)] is added to the beginning of the from1 class. runtime. in interopservices, the default value is true, indicating that the managed type is visible to com.
[Comvisible (true)]
Public partial class form1: system. Windows. Forms. Form
Conclusion:
This article briefly introduces the usage of webbrowser and implements Winform Program interaction with Web pages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.