Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run-time creation of Visual Studio form designer source code

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?

like image 649
Andrea Parodi Avatar asked Sep 15 '10 17:09

Andrea Parodi


People also ask

How do we create the form and managed at run time?

Add TextBox Control and using Property window design your TextBox. Add TreeView Control and using Property window design your TreeView. Bind Data from as DataTable return to DataGridView, ListView, ComboBox and ListBox. Bind Data From XML File as DataTable return to DataGridView, ListView, ComboBox and ListBox.

What is Form Designer in Visual Studio?

Windows Forms Designer in Visual Studio provides a rapid development solution for creating Windows Forms-based applications. Windows Forms Designer lets you easily add controls to a form, arrange them, and write code for their events. For more information about Windows Forms, see Windows Forms overview.

How it is useful while designing the form window?

The Windows Forms Designer shows the designer surface for the DemoCalculator control. In this view, you can graphically design the appearance of the control by selecting controls and components from Toolbox and placing them on the designer surface.


1 Answers

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...
}
like image 132
kyndigs Avatar answered Oct 17 '22 05:10

kyndigs