I have about 500 WinForms that are generated dynamically from XML files by an old app. I would like to automatically convert these to VB or C# code I can edit in the designer.
I can do it by hand, which would take forever...
Or write an app that loads each XML, calls the old app's xml-to-winform-builder to convert it to a completed WinForm, and examine every control's property and use StringBuilder to generate code that the designer will swallow. Which would be arduous at best.
BUT: I would like to use the same code that the designer uses. This I think will save me time with corner cases.
What I would like to do is, take an already built WinForm object, with control arrays full of child controls (created by the old apps xml-to-winform code), throw it at the same code the IDE uses to serialize the designed form to CodeDom, and get back a CodeDom list of statements I can save in a real language. Once I get the CodeDOM I'll be happy!
EDIT
This Codeproject article embodies the 'concept' of what I want to do: Start with a completed form, convert it ('serialize it') as code. However, it has two lacks: (1) it generates the .aspx page using templates/stringbuilder, and that is where the properties are (fine for webforms, but winforms serializes the properties into the actual .Designer.vb file); (2) it does this all from the ground up. I want to re-use visual studio's routines. They let you do this with many things, for example, the property grid. I'm just seeking a link to an article (maybe my google-fu was too weak), or, a short code sample from something somebody has already done.
So this may or may not fit your needs. I have a solution but it will require that you tweak your XML -> WinForms rendering.
This relies heavily on the use of this project: http://ivanz.com/files/docs/designerhosting/create-and-host-custom-designers-dot-net.html
Download the EXE (which is just a compressed version of the source with a EULA) and build the solution, I had some issues with building until I removed references to the graphing library (and the associated calls to it from the Host library). Why the Host library needs to draw graphs I'm not really sure...
Then I built a new WinForms project (can't be console because the DesignSurface fails to hook into drag/drop event). Reference the Host and Loader projects from the above project in your new winforms project.
Here is where you will undoubtably need to tweak your existing process. Merge your existing process so that it fits into this type of form building shown for my label below:
This generates your form.cs file content (see generated code below). This is an all in one file, you do not need to create a seperate form.cs.designer file to get designer support. I copied the code generated above, saved it to a .cs file, and visual studio recognized it as a winform and gave me design support.
Your copy will probably include a sub Main. I went into the Loader -> CodeGen.cs file and commented out the section related to Main, I would suggest you do the same.
edit by FastAl
Peter, you rock!!! This is pretty much exactly what I wanted. Well, I can't exactly just throw the object at it yet; using just the above method, none of the .Controls properties were filled, so I got a blank form. Also it seems like the form has to be created by GetNewHost. Fortunately my XMl-to-screen routine doesn't actually create the container, it just returns a flat list of controls that I must properly reparent (SetChildren sub). Note the comment where I have to add them to the host container for it to know about them. NOW it works perfectly!