views:

146

answers:

1

I have textbox in one of my forms and i want to call a javascript function from a javascript file...

My code is

    this.txtbox.Text = "";
    this.txtbox.Location = new Point(10, 20);
    this.txtbox.Size = new System.Drawing.Size(200, 100);
    this.txtbox.Multiline = true;
    this.Controls.Add(txtbox);
    this.txtbox1.Text = "";
    this.txtbox1.Location = new Point(220,100);
    this.txtbox1.Size  = new System.Drawing.Size(100,100);
    this.Controls.Add(txtbox1);

And my javascript.js file contains a function

function alertMsgLength() {
   alert("a");
}

How to call alertMsgLength() function to my textbox,

this.txtbox.OnKeyPress=?
this.txtbox.OnKeyDown=?

Any suggestion...

A: 

In order to call JavaScript function from C# WinForms application, you will have to host Scripting runtime within your application. Microsoft provides interfaces that will allow you to execute Windows Scripting Engine inside your application and expose your controls to it. It's not an easy thing to do, and I would not suggest doing it, unless you have a very good reason (like writing a very large, customizable application, that you want to provide API for users to change the behaviors, etc.). Take a look at those links: http://msdn.microsoft.com/en-gb/library/t9d4xf28(VS.85).aspx http://www.codeproject.com/KB/cs/csscriptengine.aspx

P.S. If you just need to call some JavaScript function from your textbox, I would highly recommend converting that JavaScript to C#. C# can do everything scripting languages can do better.

Ilya Volodin