How to generate a Form (normal windows Form, that available in C#) by using the autocode generating. (Say by using CodeDom object.)

- Wants to generate a simple form with 2 labels and 2 buttons inside; by using CodeDom object technique,

is it possible.

pls, provide a sample code if possible

Re: Visual C# General Generate Form from CodeDom


It is possible but you will be writing a bunch of code. CodeDom is great but the better route is to create a base class (the form) that you compile into your assembly as normal (or include in source form as a template). The CodeDom code you generate then creates a new class deriving from the base class. This allows you to easily test the form code without having to generate it all in the CodeDom. It also gives you a base class from which you can work when you actually instantiate an instance later on. It is faster and easier than using reflection.

So you might generate the following form class inside your assembly directly:

public class MyForm : Form




Then in the CodeDOM you'd generate this code:

public class CustomForm : MyForm



You could then compile and instantiate an instane of the custom form using code similar to this:

MyForm frm = asm.CreateInstance("CustomForm");

Michael Taylor - 6/10/07