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.
 
<html>
     <head>
          <title></title>
          <script type="text/javascript">
          function ShowMessage(message)
          {
               alert(message);
          }
          function ShowWinFormsMessage() {
               var msg = document.getElementById('txtMessage').value;
               return window.external.ShowMessage(msg);
          }
          </script>
     </head>
     <body>
          <input type="text" id="txtMessage" />
          <input type="button" value="Show Message" onclick="ShowWinFormsMessage()" />
     </body>
</html>
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()
        {
            InitializeComponent();
            webBrowser1.ObjectForScripting = new ScriptManager(this);
        }
        private void btnShowMessage_Click(object sender, EventArgs e)
        {
            object[] o = new object[1];
            o[0]=txtMessage.Text;
            object result = this.webBrowser1.Document.InvokeScript("ShowMessage", o);
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.webBrowser1.Navigate(@"E:\Projects\2010\WebBrowserJavaScriptExample\WebBrowserJavaScriptExample\TestPage.htm");
        }
        [ComVisible(true)]
        public class ScriptManager
        {
            frmMain _form;
            public ScriptManager(frmMain form)
            {
                _form = form;
            }
            public void ShowMessage(object obj)
            {
                MessageBox.Show(obj.ToString());
            }
        }
    }
}

License

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

Share

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 
GeneralRe: Using ScritManager Pinmemberkaminski00117-Jun-13 17:33 
QuestionWhat about implementing IActiveScriptSite? PinmemberSergeyT216-Nov-10 7:22 
QuestionHaving Trouble Testing This - can you help? Pinmemberdpminusa16-Nov-10 3:21 
AnswerRe: Having Trouble Testing This - can you help? PinmemberKanasz Robert16-Nov-10 10:45 
GeneralRe: Having Trouble Testing This - can you help? Pinmemberdpminusa16-Nov-10 20:52 
GeneralRe: Having Trouble Testing This - can you help? PinmemberKanasz Robert16-Nov-10 21:50 
Hi,
 
I'm so sorry, but I didn't receive mail from you.
I'm planning to create mobile web page for one customer (it is personal agency) using MVC... it is challenge for me, because I must learn MVC and I think it will be interesting to optimize application for various mobile devices and browsers. And what about you?
GeneralRe: Having Trouble Testing This - can you help? Pinmemberdpminusa16-Nov-10 21:59 
GeneralRe: Having Trouble Testing This - can you help? PinmemberKanasz Robert16-Nov-10 22:19 
GeneralRe: Having Trouble Testing This - can you help? Pinmemberdpminusa17-Nov-10 2:00 
GeneralRe: Having Trouble Testing This - can you help? PinmemberKanasz Robert17-Nov-10 2:10 
GeneralRe: Having Trouble Testing This - can you help? Pinmemberdpminusa17-Nov-10 2:21 
GeneralRe: Having Trouble Testing This - can you help? PinmemberKanasz Robert16-Nov-10 22:22 

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
Web03 | 2.8.1411019.1 | Last Updated 14 Nov 2010
Article Copyright 2010 by Kanasz Robert
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid