2

How could I execute VBScript's code from TextBox control placed in C# application?

like , let's assume that I have a C# Windows Application(form) and has two controls!

  1. textbox (txtCode)
  2. button (btnExecute)

txtCode has VBScript code and I want when clicking at btnExecute to execute the VBScript code!!

Bastardo
  • 4,144
  • 9
  • 41
  • 60
Muhamad
  • 31
  • 2

4 Answers4

3

You can pass VBS/JS directly to the scripting runtime & pass code & objects around.

Add a ref to the Microsoft Scripting Control (COM) then you can;

MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
object[] anyParams = { "Bob"};
string expr = @"function foo(arg)
    dim x: x = ""Hello "" & arg 
    msgbox x
    foo = 12345 
    End function";

sc.Language = "VBScript";
sc.AddCode(expr);

object result = sc.Run("foo", ref anyParams);

//also
sc.Reset();
result = sc.Eval("1 + 2 / 3 + abs(-99)");
Alex K.
  • 171,639
  • 30
  • 264
  • 288
0

You will need to use WebBrowser Control to do that.

And since i don't think you can inject VBScript code in the loaded page. So what you can do is Create a Temp .html page, save your TextBox's script in it and then Load it in the the WebBrowser Control.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
  • 1
    VBScript is not just a browser scripting language, it can be used with [Windows Script Host](http://msdn.microsoft.com/en-us/library/9bbdkx3k(v=VS.85).aspx), as well as server-side ASP –  Apr 19 '11 at 12:56
0

Couldn't you simply write it to a file and execute it using the default Windows behavior or something like that? It's the only way I can think of.

Simon Dufour
  • 184
  • 7