Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

Comments and Discussions

Questionmy QUESTION.. Pinmemberbentroy25-Nov-13 18:11 
GeneralMy vote of 5 PinmemberPablo Aliskevicius20-Jun-13 2:22 
GeneralRe: My vote of 5 PinmvpKanasz Robert21-Jun-13 23:22 
Questionthanks great job Pinmembermrx10012-May-12 8:45 
AnswerRe: thanks great job PinmvpKanasz Robert21-Jun-13 23:23 
GeneralReason for my vote of 5 Thx. PinmemberPranit Kothari2-Jan-12 15:56 
GeneralReason for my vote of 5 good job man Pinmemberronen4415-Nov-10 22:51 
GeneralThanks for posting this it helped me... Pinmemberxzz019519-Apr-11 19:04 
GeneralRe: Thanks for posting this it helped me... PinmvpKanasz Robert21-Jun-13 23:23 
GeneralUsing ScritManager Pinmemberchaitanyasingh10-Dec-10 21:30 

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

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

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