torsdag 24 februari 2011

Att använda Javascript i Windows Forms

Ett litet exempel på hur man kan använda Javascript i Windows Forms

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 MSScriptControl;

namespace Blog.Script
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ScriptControlClass script = new ScriptControlClass();
            script.Language = "JScript";
            script.AddCode("var v = 10;");

            StringBuilder js = new StringBuilder();
            js.AppendLine("function max(x,y){ return x > y ? x : y; }");
            js.AppendLine("max(v,3);");

            string result = "";

            try
            {
                result = script.Eval(js.ToString()).ToString();
            }
            catch
            {
                result = script.Error.Line + "," + script.Error.Column + " " script.Error.Description;
            }
            MessageBox.Show(result);
        }
    }
}

    1 kommentar:

    Anonym sa...

    Very effectively written information. It will likely be priceless to anybody who usess it, including myself. Keep up the great work – for positive i will check out more posts.