How to execute VBScript command within textbox from C# application?
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!
- textbox (txtCode)
- button (btnExecute)
txtCode has VBScr开发者_运维百科ipt code and I want when clicking at btnExecute to execute the VBScript code!!
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)");
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.
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.
Check out this two articles:
http://msdn.microsoft.com/en-gb/magazine/cc301954.aspx
http://msdn.microsoft.com/en-us/library/ms974577.aspx
精彩评论