Click here to Skip to main content
12,881,367 members (27,585 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


34 bookmarked
Posted 14 Nov 2010

Calling JavaScript function from WinForms and vice-versa

, 14 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
I'be been working on application that needs to load page and display it in WebBrowser control. Second requirement was to allow interaction between WinForms application and web page. After a couple of minutes of googling I found some important information that helped me to solve my problem. Here I will show a sample application where JavaScript function is called from WinForms application.

Hole process can be described by following steps:

  • Create Windows Forms application

  • Add WebBrowser control into Form

  • Create web page with JavaScript function you want to call

Calling JavaScript function from WinForms application isn't so difficult. WebBrowser constrol has property Document which gets an HtmlDocument representing the Web page currently displayed in the WebBrowser control.

You can use this property, in combination with the ObjectForScripting property, to implement two-way communication between a Web page displayed in the WebBrowser control and your application. Use the HtmlDocument.InvokeScript method to call script methods implemented in a Web page from your client application code. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to an object that you specify for the ObjectForScripting property.

          <script type="text/javascript">
          function ShowMessage(message)
          function ShowWinFormsMessage() {
               var msg = document.getElementById('txtMessage').value;
               return window.external.ShowMessage(msg);
          <input type="text" id="txtMessage" />
          <input type="button" value="Show Message" onclick="ShowWinFormsMessage()" />

Two JavaScript functions are in sample page. function ShowMessage(message) is called from WinForms application and function ShowWinFormsMessage call ShowMessage(msg) C# function.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WebBrowserJavaScriptExample
    public partial class frmMain : Form
        public frmMain()
            webBrowser1.ObjectForScripting = new ScriptManager(this);
        private void btnShowMessage_Click(object sender, EventArgs e)
            object[] o = new object[1];
            object result = this.webBrowser1.Document.InvokeScript("ShowMessage", o);
        private void frmMain_Load(object sender, EventArgs e)
        public class ScriptManager
            frmMain _form;
            public ScriptManager(frmMain form)
                _form = form;
            public void ShowMessage(object obj)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kanasz Robert
Architect The Staffing Edge & Marwin Cassovia Soft
Slovakia Slovakia
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist

Open source projects: DBScripter - Library for scripting SQL Server database objects

Please, do not forget vote

You may also be interested in...

Comments and Discussions

Questionthanks great job Pin
mrx10012-May-12 7:45
membermrx10012-May-12 7:45 
AnswerRe: thanks great job Pin
Kanasz Robert21-Jun-13 22:23
mvpKanasz Robert21-Jun-13 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 14 Nov 2010
Article Copyright 2010 by Kanasz Robert
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid