2

I wanted to learn about building files with c# so I created a Windows Forms Application and I created this code/form. But It did not seem to work.
.

enter image description here

using System;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace teztie
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "*.exe |*.exe";
            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                build(sfd.FileName, textBox1.Text, textBox2.Text);
            }
        }

        private void build(string output, string msg, string name)
        {
            CompilerParameters p = new CompilerParameters();
            p.GenerateExecutable = true;
            p.ReferencedAssemblies.AddRange(new string[] { "System.dll", "System.Windows.Forms.dll"});
            p.OutputAssembly = output;
            p.CompilerOptions = "/t:winexe";

            string source = Properties.Resources.source;
            string errors = string.Empty;

            source = source.Replace("[MSG]", msg);
            source = source.Replace("[NAME]", name);

            CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(p, source);

            if (results.Errors.Count > 0)
            {
                foreach (CompilerError err in results.Errors)
                {
                    errors += "Error: " + err.ToString() + "\r\n\r\n";
                }
            }
            else
            {
                errors = "Successfully built:\n" + output;
            }

            MessageBox.Show(errors, "Build", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}

Source.Txt:

using System;
using System.Windows.Forms;

namespace code
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("[msg]",  "[title]" );
        }
    }
}

But when I click the compile button it gives me an error. The name InitializeComponent does not exist in the current context.

enter image description here

Gewoo
  • 437
  • 6
  • 19
  • 3
    Try putting everything in the same namespace – edtheprogrammerguy Feb 19 '16 at 21:32
  • Possible duplicate of http://stackoverflow.com/questions/24693000/the-name-initializecomponent-does-not-exist-in-the-current-context-in-wpf-appl http://stackoverflow.com/questions/17853898/the-name-initializecomponent-does-not-exist-in-the-current-context-cannot-get http://stackoverflow.com/questions/6925584/the-name-initializecomponent-does-not-exist-in-the-current-context – Anwar Ul-haq Feb 19 '16 at 21:41
  • You have changed the namespace in your Form1.cs file but this file contains a partial class. The remainder of your class is in a file named Form1.designer.cs. You will find the InitializeComponent there, but the namespace now is different and the compiler cannot find it. Fix the namespace also in the designer.cs file – Steve Feb 19 '16 at 21:43

1 Answers1

3

The constructor is calling a method called "InitializeComponent", but that method is not defined in your class. Typically, InitializeComponent is used by the VS designer to hold code needed to apply your selections in the designer at runtime. If you copied this form from an example, either add an empty method called "InitializeComponent" to this class or remove this method call from your constructor.

Eric W
  • 579
  • 4
  • 14
  • This should be the accepted answer. One is used that InitializeComponent() is a call to a routine which exists automatically, but this is not the case, if the designer was not used to create the form, but the form was copied by cut&paste, or other. – Philm Jun 06 '16 at 22:19