views:

244

answers:

3

I'm refactoring a program containing a lot of forms created dynamically from run-time informations.

To reduce the level of complexity of the system, I thought to write individual code files for each of these forms. Since the forms are many, I'm thinking for a way to automate the process of creation of the forms source code files from data collected at run-time.

E.g. if i have a run-time instance of a form called EditPeople, I want to create the source code of EditPeople.designer.cs, so that then I can edit the form in windows form designer.

Do you know if there is some framework or tool that can simplify this task?

+1  A: 

I just saw this question, for future reference you could try something like this:

public static List<Control> GetAllControls(IList ctrls)
{
   List<Control> FormCtrls = new List<Control>();
   foreach (Control ctl in ctrls)
   {
      FormCtrls .Add(ctl);
      List<Control> SubCtrls = GetAllControls(ctl.Controls);
      FormCtrls .AddRange(SubCtrls);
   }
   return FormCtrls;
} 

You can use this function like this:

List<Control> ReturnedCtrls = GetAllControls(MyForm.Controls);

The when you have a list of all the controls you can do something like this:

foreach(Control ctrl in ReturnedCtrls)
{
   // Generate Designer Source using ctrl properties
   ctrl.Left
   ctrl.Top
   // etc...
}
kyndigs
A: 

Hi, just a thought.

If the intention is to recreate the form's code I think using .net reflector http://www.red-gate.com/products/reflector/ along with its addon FileDisassembler denisbauer.com/NETTools/FileDisassembler.aspx (I cannot post more than 1 hyperlink :) ) would help you in decompiling the full project in short time.

Don't you think?

CodeCanvas
A: 

2 CodeCanvas - No way. Reflector gives you a class while the task is to serialize all instances of all given classes. For example, class contains code:

for(int i=0; i<2;i++){
Controls.Add(new Button());
}

task is to make *.Designer.cs with following code:

//...
InitializeComponent()
{
    //...
    button1 = new Button();
    button2 = new Button();
    button3 = new Button();
    //...
    this.Controls.Add(button1);
    this.Controls.Add(button2);
    this.Controls.Add(button3);
}
//...

and that is definitely not what reflector gives you.

CodeDom serialization could make the trick, but it acts following its own rules and unlikely would give the complete snapshot of form.