Introduction
This article explains how to use a JavaScript function in a Windows application C#. A previous article explained how to call a JavaScript function in C# code. The following are the points:
- How to use a JavaScript function in a Windows application
- How to call a JavaScript function in C# code.
I have explained these points using an example. My example uses a web browser control of a Windows Forms application.
Step 1- Initially create a new window based project. Execute Visual Studio then:
"File" -> "New" -> "Project..." then select "Windows" -> "Windows Forms Application".
Step 2- Then drag and drop a web browser control to the Windows Forms form and also drag and drop a Print Dialog Control onto the form.
Step 3- Then go to "Application" => "Add New Item" => "Add HTML page" to add a page to the debug folder of the application.
Step 4- Create a JavaScript function on the HTML page. Here I need to write two functions, one function without a parameter and another one with a parameter.
Step 5- Write this code in Form Constructor.
Step 6- Write this code for the Form Load event:
Step 7- Write this code in the web browser control's DocumentCompleted method.
Step 8- Drag two Buttons and one TextBox control to your form. My form design now looks like this:
Step 9:- After designing write this code for the button Click event.
Output
When this button is clicked you will get the following message:
The second button's output is: