* Update webbrowser sample As per suggestions https://github.com/dotnet/winforms/issues/4443#issuecomment-759333446 Resolves dotnet/winforms#4443 * fixup! Update webbrowser sample * fixup! Update webbrowser sample * Update dotnet-desktop-guide/framework/winforms/controls/implement-two-way-com-between-dhtml-and-client.md Co-authored-by: Andy De George <[email protected]> Co-authored-by: Andy De George <[email protected]>
8.2 KiB
title, ms.date, dev_langs, f1_keywords, helpviewer_keywords, ms.assetid
| title | ms.date | dev_langs | f1_keywords | helpviewer_keywords | ms.assetid | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| How to: Implement Two-Way Communication Between DHTML Code and Client Application Code | 03/30/2017 |
|
|
|
55353a32-b09e-4479-a521-ff3a5ff9a708 |
How to: Implement Two-Way Communication Between DHTML Code and Client Application Code
You can use the xref:System.Windows.Forms.WebBrowser control to add existing dynamic HTML (DHTML) Web application code to your Windows Forms client applications. This is useful when you have invested significant development time in creating DHTML-based controls and you want to take advantage of the rich user interface capabilities of Windows Forms without having to rewrite existing code.
The xref:System.Windows.Forms.WebBrowser control lets you implement two-way communication between your client application code and your Web page scripting code through the xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A and xref:System.Windows.Forms.WebBrowser.Document%2A properties. Additionally, you can configure the xref:System.Windows.Forms.WebBrowser control so that your Web controls blend seamlessly with other controls on your application form, hiding their DHTML implementation. To seamlessly blend the controls, format the page displayed so that its background color and visual style match the rest of the form, and use the xref:System.Windows.Forms.WebBrowser.AllowWebBrowserDrop%2A, xref:System.Windows.Forms.WebBrowser.IsWebBrowserContextMenuEnabled%2A, and xref:System.Windows.Forms.WebBrowser.WebBrowserShortcutsEnabled%2A properties to disable standard browser features.
To embed DHTML in your Windows Forms application
-
Set the xref:System.Windows.Forms.WebBrowser control's xref:System.Windows.Forms.WebBrowser.AllowWebBrowserDrop%2A property to
falseto prevent the xref:System.Windows.Forms.WebBrowser control from opening files dropped onto it.[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#1] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#1]
-
Set the control's xref:System.Windows.Forms.WebBrowser.IsWebBrowserContextMenuEnabled%2A property to
falseto prevent the xref:System.Windows.Forms.WebBrowser control from displaying its shortcut menu when the user right-clicks it.[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#2] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#2]
-
Set the control's xref:System.Windows.Forms.WebBrowser.WebBrowserShortcutsEnabled%2A property to
falseto prevent the xref:System.Windows.Forms.WebBrowser control from responding to shortcut keys.[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#3] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#3]
-
Set the xref:System.Windows.Forms.WebBrowser.ObjectForScripting%2A property in the form's constructor or override the xref:System.Windows.Forms.Form.OnLoad%2A method.
The following code uses the form class itself for the scripting object.
[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#4] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#4]
-
Implement your scripting object.
[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#5] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#5]
-
Use the
window.externalobject in your scripting code to access public properties and methods of the specified object.The following HTML code demonstrates how to call a method on the scripting object from a button click. Copy this code into the BODY element of an HTML document that you load using the control's xref:System.Windows.Forms.WebBrowser.Navigate%2A method or that you assign to the control's xref:System.Windows.Forms.WebBrowser.DocumentText%2A property.
<button onclick="window.external.Test('called from script code')"> call client code from script code </button> -
Implement functions in your script code that your application code will use.
The following HTML SCRIPT element provides an example function. Copy this code into the HEAD element of an HTML document that you load using the control's xref:System.Windows.Forms.WebBrowser.Navigate%2A method or that you assign to the control's xref:System.Windows.Forms.WebBrowser.DocumentText%2A property.
<script> function test(message) { alert(message); } </script> -
Use the xref:System.Windows.Forms.WebBrowser.Document%2A property to access the script code from your client application code.
For example, add the following code to a button xref:System.Windows.Forms.Control.Click event handler.
[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#8] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#8]
-
When you are finished debugging your DHTML, set the control's xref:System.Windows.Forms.WebBrowser.ScriptErrorsSuppressed%2A property to
trueto prevent the xref:System.Windows.Forms.WebBrowser control from displaying error messages for script code problems.[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#9] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#9]
Example
The following complete code example provides a demonstration application that you can use to understand this feature. The HTML code is loaded into the xref:System.Windows.Forms.WebBrowser control through the xref:System.Windows.Forms.WebBrowser.DocumentText%2A property instead of being loaded from a separate HTML file.
[!code-csharpSystem.Windows.Forms.WebBrowser.ObjectForScripting#0] [!code-vbSystem.Windows.Forms.WebBrowser.ObjectForScripting#0]
Compiling the Code
This code requires:
- References to the System and System.Windows.Forms assemblies.