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();
}
Does this window.external.MethodToCallFromScript() works similarly if created in VB.NET? Thank you, even this code is very helpful
| Posted 16 years, 3 months agoYes it should work, since this is part of the .net Framework, but it won’t work in .net Compact Framework.
| Posted 16 years, 3 months agothanks for the prompt reply. how would i make a vb.net class com visible? thanks again.
| Posted 16 years, 3 months agoImports 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
| Posted 16 years, 3 months ago