3

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,

user1291401
  • 264
  • 1
  • 6
  • 18
  • 1
    have you included the part where InitializeComponent is declared ? – Selman Genç Sep 25 '14 at 09:34
  • @Selman22 - no .. but Form1 is dyamically generated.. so how do i include it. – user1291401 Sep 25 '14 at 09:35
  • do you place controls.. from toolbox..on dynamically created Form. –  Sep 25 '14 at 09:38
  • 1
    You are not doing *all*, what winforms designer does. Remove `InitializeComponent()` call (it's automatically generated by designer, but you are not using one) and perform initialization yourself: set form properties, add controls, set control properties, add event handlers, take care of disposing *components*. *User drags and drops the control on this form* - you are basically making designer yourself, then its worth to look deep into designer code. – Sinatr Sep 25 '14 at 09:55
  • @Sinatr - will try this method also.. – user1291401 Sep 25 '14 at 10:10

1 Answers1

1

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

Greenhorn
  • 74
  • 6