--- title: "How to: Implement Two-Way Communication Between DHTML Code and Client Application Code" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" f1_keywords: - "WebBrowser.ObjectForScripting" - "WebBrowser.Document" helpviewer_keywords: - "WebBrowser control [Windows Forms], examples" - "communications [Windows Forms], DHTML and client applications" - "examples [Windows Forms], WebBrowser control" - "WebBrowser control [Windows Forms], communication between DHTML and client application" - "DHTML [Windows Forms], embedding in Windows Forms" ms.assetid: 55353a32-b09e-4479-a521-ff3a5ff9a708 --- # How to: Implement Two-Way Communication Between DHTML Code and Client Application Code You can use the 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 control lets you implement two-way communication between your client application code and your Web page scripting code through the and properties. Additionally, you can configure the 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 , , and properties to disable standard browser features. ## To embed DHTML in your Windows Forms application 1. Set the control's property to `false` to prevent the control from opening files dropped onto it. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#1)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#1)] 2. Set the control's property to `false` to prevent the control from displaying its shortcut menu when the user right-clicks it. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#2)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#2)] 3. Set the control's property to `false` to prevent the control from responding to shortcut keys. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#3)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#3)] 4. Set the property in the form's constructor or override the method. The following code uses the form class itself for the scripting object. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#4)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#4)] 5. Implement your scripting object. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#5)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#5)] 6. Use the `window.external` object 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 method or that you assign to the control's property. ```html ``` 7. 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 method or that you assign to the control's property. ```html ``` 8. Use the property to access the script code from your client application code. For example, add the following code to a button event handler. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#8](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#8)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#8](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#8)] 9. When you are finished debugging your DHTML, set the control's property to `true` to prevent the control from displaying error messages for script code problems. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#9](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#9)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#9](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#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 control through the property instead of being loaded from a separate HTML file. [!code-csharp[System.Windows.Forms.WebBrowser.ObjectForScripting#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/CS/form1.cs#0)] [!code-vb[System.Windows.Forms.WebBrowser.ObjectForScripting#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.WebBrowser.ObjectForScripting/vb/form1.vb#0)] ## Compiling the Code This code requires: - References to the System and System.Windows.Forms assemblies. ## See also - - - - [WebBrowser Control](webbrowser-control-windows-forms.md)