Hi all,
I need something to know please help me.
Can I use the code in textbox??
For Ex:
I have a textbox (Name: code) to write the code and two other to enter values.
I write these in my first textbox :
Convert.ToDouble(value1.Text)+ Convert.ToDouble(value2.Text);
and when i click the button I want it to be calculated. and see the result as a string on a laber or whatever.
I read soo much articles but still I cant do it. Please help me. Thanks to all.
Loading
Guest UserPosted Oct 31, 2013, 3:05 PM
Guest UserPosted Oct 31, 2013, 3:00 PM
Thank you sooooo much for your solution and interest on my problem. I wrote all the codes behind my Button and it worked well. Thank you again. You are the one who utnderstood what I wanted to do. But I need your advises one more time.
I wrote string sCSCode = codeBox.Text;
at the begining of the codes then I ran the program and I wrote these in the codeBox :
Convert.ToDouble(value1.Text)+Convert.ToDouble(value2.Text);
( I have two other textBoxes to write some numbers.. )
I take this Error : ERROR: The name 'value1' does not exist in the curent context.
Rajesh Kumar JhaPosted Oct 31, 2013, 11:12 AM
I have got some help from the following article
http://www.codeproject.com/Articles/11939/Evaluate-C-Code-Eval-Function
and here is the Same Code that i have modified a bit for you.
private void button1_Click(object sender, EventArgs e)
{
string sCSCode = @"Convert.ToDouble(10)+ Convert.ToDouble(20);";
CSharpCodeProvider c = new CSharpCodeProvider();
ICodeCompiler icc = c.CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.ReferencedAssemblies.Add("system.xml.dll");
cp.ReferencedAssemblies.Add("system.data.dll");
cp.ReferencedAssemblies.Add("system.windows.forms.dll");
cp.ReferencedAssemblies.Add("system.drawing.dll");
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;
StringBuilder sb = new StringBuilder("");
sb.Append("using System;\n");
sb.Append("using System.Xml;\n");
sb.Append("using System.Data;\n");
sb.Append("using System.Data.SqlClient;\n");
sb.Append("using System.Windows.Forms;\n");
sb.Append("using System.Drawing;\n");
sb.Append("namespace CSCodeEvaler{ \n");
sb.Append("public class CSCodeEvaler{ \n");
sb.Append("public object EvalCode(){\n");
sb.Append("return " + sCSCode + "; \n");
sb.Append("} \n");
sb.Append("} \n");
sb.Append("}\n");
CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString());
if (cr.Errors.Count > 0)
{
MessageBox.Show("ERROR: " + cr.Errors[0].ErrorText,
"Error evaluating cs code", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
System.Reflection.Assembly a = cr.CompiledAssembly;
object o = a.CreateInstance("CSCodeEvaler.CSCodeEvaler");
Type t = o.GetType();
MethodInfo mi = t.GetMethod("EvalCode");
object s = mi.Invoke(o, null); // See Your Result Here
}
Guest UserPosted Oct 31, 2013, 8:11 AM
Rajesh Kumar JhaPosted Oct 31, 2013, 6:11 AM
http://support.microsoft.com/kb/304655
http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments