GLOBEtech



Call Managed C# Method from JavaScript Code

After searching quite long to find a way to call a C# Method from any JavaScript Code which is hostet in a WinForms Webbrowser Control, I found this one here:

C# Code
[ComVisible(true)]
public class ScriptManager
{
Form mForm;

public ScriptManager(Form form)
{
mForm= form;
public void MethodToCallFromScript()
{
mForm.DoSomething()
}
}
}

public class MyForm : Form
{
private Webbrowser mBrowserCtrl;

public MyForm()

{

mBrowserCtrl = new Webbrowser();
mBrowserCtrl.ObjectForScripting= new ScriptManager(this);

}

public void DoSomething(){}

}

JavaScript Code
function HandleSomething()
{
window.external.MethodToCallFromScript();
}

About these ads

Trackbacks & Pingbacks

Comments

  1. * kashif says:

    Does this window.external.MethodToCallFromScript() works similarly if created in VB.NET? Thank you, even this code is very helpful

    | Reply Posted 6 years, 8 months ago
  2. * globetech says:

    Yes it should work, since this is part of the .net Framework, but it won’t work in .net Compact Framework.

    | Reply Posted 6 years, 8 months ago
  3. * kashif says:

    thanks for the prompt reply. how would i make a vb.net class com visible? thanks again.

    | Reply Posted 6 years, 8 months ago
  4. * braintech says:


    Imports System.Runtime.InteropServices

    <ComVisible(False)> _
    Class SampleClass
    End Class

    for more details have a look at: http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute(VS.71).aspx

    | Reply Posted 6 years, 8 months ago


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: