How to execute VBScript command within textbox fro

2019-02-19 05:31发布

问题:

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!!

回答1:

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)");


回答2:

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.



回答3:

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.



回答4:

Check out this two articles:
http://msdn.microsoft.com/en-gb/magazine/cc301954.aspx
http://msdn.microsoft.com/en-us/library/ms974577.aspx