Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-Generating a Winforms form from a C# POCO Class

Is there some open source code already written out there somewhere that will accept a class with properties and validation attributes, and output a Windows form with controls that correspond to those properties?

Examples:

public bool IsRed { get; set; }

produces a checkbox with an Is Red? label.

public int NumberOfDays { get; set; }

produces a text box with a label called Number of Days and restricts input to numeric characters only.

[Required]
public Color Color { get; set; }

where Color is an enum of the form

public enum Color
{
    Red,
    Green,
    Blue
}

produces a combo box with the list populated with Red Green and Blue, and makes it required.

Ideally, the generated code includes a method that accepts an instance of my class and prepopulates the controls in the winform with the values in the properties of my instance. Correspondingly, another method saves the existing values in the controls to an instance of my class.

Is there something like that available?


Please note: I am not looking for:

  1. An ORM or DAL
  2. A generalized tool like CodeSmith, unless it's free and open-source
  3. WPF or ASP.NET code, it needs to be Winforms
  4. NetTiers or any similar type of complete application framework, unless I can parse out the small part of it that applies specifically to this scenario.
like image 358
Robert Harvey Avatar asked Nov 14 '22 14:11

Robert Harvey


1 Answers

It is 'quite easy' to extract the functionality of the PropertyGrid into your own hosted controls, but that would only cover half your requirement. At a minimum, read up on TypeConverter, TypeDescriptor and PropertyDescriptor.

Also have a look at: codeproject.com/KB/custom-controls/xacc_propertygrid.aspx, this basically exposes the PropertyGrid's functionality to a web page, but it will give you some ideas.

I know this is not a complete answer, but should point one in the right direction to write/author such a tool.

It would be nice if such a scaffolding tool existed for WinForms :)

like image 90
leppie Avatar answered Dec 22 '22 07:12

leppie