0

I am trying to produce a Windows Application Form via Codedom. I found a great example showing me how to do this for a Console Application but I can't seem to make this work for a Windows Form.

Here is what I have so far:

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
            string Output = "Out.exe";
            Button ButtonObject = (Button)sender;

            textBox2.Text = "";
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            //Make sure we generate an EXE, not a DLL
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

Textbox1.text Contains the following:

Public Class Form1: Form
{

}

I'm really not sure what else to put... I am very new to this stuff and I can't seem to understand the articles I came across.

2
  • 1
    I have no idea if this will work, but have you tried to make a forms application, then copy+paste that code in? Commented May 26, 2011 at 4:02
  • @ soandos Yes I have but I get all sorts of errors. Too many to list... Commented May 26, 2011 at 14:39

2 Answers 2

2

If you're new to CodeDom I strongly suggest you to use Linq2CodeDom which allows you to write your code in expressions which later will be translated through CodeDom into VB or C# code. With this library you can write something like this:

public void Generate() 
{
    var c = new CodeDomGenerator();
    c.AddNamespace("Samples")
     .AddClass("Form1")
     .AddMethod(MemberAttributes.Public | MemberAttributes.Static, ()=>"YourMethodName", Emit.stmt(() => MessageBox.Show("Method Body")));
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Interesting link. Do you know whether this library can be used to target .NET 2.0 framework?
1

Assuming that you are actually able to create the EXE at the moment (as you are missing some using statements in your Form1 declaration) I would start by adding the entry point, the static Main method that creates and displays a new Form1 instance:

using System;
using System.Windows.Forms;

public class Form1 : Form
{
    public static void Main(string[] args)
    {
        var form1 = new Form1();
        Application.Run(form1);
    }
}

That should at least get a window appearing when you run your generated EXE. @soandos also has a good point, you should be able to copy and paste from the source created when you create a form in Visual Studio, althought you should remember that VS2008+ uses partial classes so you need to combine the contents of Form1.cs and Form1.Designer.cs.

2 Comments

I got your code to work by adding the following parameters: parameters.ReferencedAssemblies.Add("System.dll"); parameters.ReferencedAssemblies.Add("System.Data.dll"); parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); However, when I run the generated executable, a command prompt still opens up before the form opens up. Any ideas as to why this happens?
I just found this thread (social.msdn.microsoft.com/Forums/en/winforms/thread/…), last post says to set parameters.CompilerOptions = "/t:winexe";. I haven't tested that, I've only ever used CodeDom to generate DLLs, but it sounds good. CompilerParameters.CompilerOptions is effectively passed to the compiler (csc.exe) as a command line option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.