I have simple windows app where a dynamically Win Form is created and displayed along with tool-box. User drags and drops the control on this dynamically created form and accordingly writes the code. Below is not entire code but piece where me facing issue. I'm trying to compile the code written by user at runtime but it gives me error "At Form 0 -> The name 'InitializeComponent' does not exist in the current context Line (12) error :CS0103"
// small piece of code
string SecondLine = @"public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}";
Form1 frm =new Form1();
frm.textBox1.Text = "using System;" + Environment.NewLine
+ "using System.IO;" + Environment.NewLine + "using System.Drawing;" + Environment.NewLine + "using System.Windows.Forms;" + Environment.NewLine + Environment.NewLine + "namespace MiniCompiler{" + Environment.NewLine + Environment.NewLine;
frm.textBox1.Text = frm.textBox1.Text + SecondLine.ToString();
frm.textBox1.Text = frm.textBox1.Text + Environment.NewLine + Environment.NewLine + "static class Program{" + Environment.NewLine + " [STAThread] " + Environment.NewLine + "static void Main()" + "{" + Environment.NewLine;
string firstLine = "Application.EnableVisualStyles(); " + Environment.NewLine + "Application.SetCompatibleTextRenderingDefault(false);" + Environment.NewLine + "Application.Run(new Form1());";
frm.textBox1.Text = frm.textBox1.Text + Environment.NewLine + firstLine;
frm.textBox1.Text = frm.textBox1.Text.ToString() + Environment.NewLine + "}" ;
// to compile code
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler();
CompilerResults result = compiler.CompileAssemblyFromSource(param,code);
I'm really not sure what could be wrong in compiling Winform here .
Thanks,