C # interoperability between winform and client scripts

Source: Internet
Author: User

Using system;
Using system. Windows. forms;
Using system. Web;
Using system. Security. permissions;

Namespace windowsformsapplication2
{
[Permissionset (securityaction. Demand, name = "fulltrust")]
[System. runtime. interopservices. comvisibleattribute (true)]

Public partial class jscallwinfun: Form
{
Private webbrowser webbrowser1 = new webbrowser ();
Private button button1 = new button ();

// [stathread]
// public static void main ()
// {
// application. enablevisualstyles ();
// application. run (New form1 ();
//}

Public jscallwinfun ()
{< br> button1.text = "call script code from client code";
button1.dock = dockstyle. top;
button1.click + = new eventhandler (button#click);
webbrowser1.dock = dockstyle. fill;
controls. add (webbrowser1);
controls. add (button1);
LOAD + = new eventhandler (form1_load);
}

Private void form1_load (Object sender, eventargs E)
{// Client script call winfrom method example
Webbrowser1.allowwebbrowserdrop = false;
Webbrowser1.iswebbrowsercontextmenuenabled = true;
Webbrowser1.webbrowsershortcutsenabled = false;
Webbrowser1.objectforscripting = this;
// Uncomment the following line when you are finished debugging.
// Webbrowser1.scripterrorssuppressed = true;

Webbrowser1.documenttext =
"<HTML> "Function test (Message) {alert (Message);}" +
"</SCRIPT> "Onclick = \" Window. External. Test ('Called from script Code') \ ">" +
"Call client code from script code </button>" +
"</Body> }

Public void test (string message)
{
MessageBox. Show (message, "client code ");
}

private void button#click (Object sender, eventargs e)
{< br> // winfrom client call script example
webbrowser1.document. invokescript ("test",
New String [] {"called from client code "});
}< BR >}< br>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.