6

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?

4
  • 1
    Link: oreillynet.com/windows/blog/2004/10/… Commented Sep 15, 2010 at 17:58
  • Thank you. I'll try to use CodeDom objects to write source files. Commented Sep 15, 2010 at 18:47
  • Any luck with solving this? CodeDom sounds like possible solution, another thing to search for is "design-time serialization" Commented Sep 28, 2010 at 18:04
  • @DK: yes, I investigated CodeDom and it seem to be possible, but since it appear also to be too complicated, I finally choose to manually recreate the forms... Commented Sep 29, 2010 at 7:15

3 Answers 3

1

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... } 
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.