user1291401
user1291401

Reputation: 264

dynamic code compilation of winform gives error in C#

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,

Upvotes: 3

Views: 508

Answers (1)

Greenhorn
Greenhorn

Reputation: 74

I think the error-message is right. I can't see where your InitializeComponent()-Method, witch is called in constructor of the Form1-Class, is defined.

Because a Form is generated as a partial class, there could be, (and in fact there are per default) more than one file, witch contains the members of that class. Per default you got two files. In your case Form1.cs and Form1.Designer.cs. Both together describe the class Form1.

The Method InitializeComponent is not inherited. It is defined in the same class, just in an other file.

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Text = "Form1";
    }

You could copy this method from the other partial part of the Form1-Class in to your SecondLine-String. Then it should work, i think.

Look in this file

Upvotes: 1

Related Questions