Quantcast
Channel: RLV Blog
Viewing all articles
Browse latest Browse all 79

Introducing DynForm, a c# library for semi-automatic WinForms dialogs

$
0
0

Application development often involves creating dialogs where the user is presented a form to enter or edit some sort of data. DynForm aims to make development of this a little bit easier. By implementing a simple interface into your data entities, the DynForm library will be able to semi-automatically build a form and display it to the user to fill in.

Source Code

You can download the DynForm source code from GitHub. A sample application is included, showing how to use most features. Everything is written in c#. Both the sample and library code is well documented. It is released as open source, licensed with GPL3.

Screenshots

Here are some example screenshots of what DynForm dialogs can look like (taken from a real application):

dynform3

A typical form for editing person details.

dynform2

When selecting items to add to a list, a separate dialog with filtering options is shown.

dynform1

DynForm can validate data and display both errors and warnings to the user.

How to use

Lets say you have a dog entity class:

class Dog
{
	public string NameProp { get; set; }
	public string OwnerProp { get; set; }
	public bool BarksProp { get; set; }
}

First add the DynForm namespace to your class. Then add the IDynFormEntity interface and the methods required by this interface:

class Dog : IDynFormEntity
{
	public string NameProp { get; set; }
	public string OwnerProp { get; set; }
	public bool BarksProp { get; set; }

	// Methods and properties required by IDynFormEntity
	public bool bSaveData { get; set; }
	public string Validate( string propertyName )
	{
		return "";
	}
	public void SetupFormFields( DynForm.DynForm f )
	{
	}
}

DynForm is not fully automatic, so you have to tell it what to show in the form. My goal was to make it fast and easy to use, yet still let the developer be in charge of the design. Modify the  SetupFormFields() method as such:

public void SetupFormFields( DynForm.DynForm f )
{
	f.AddLayoutTextBox( "Name", "NameProp" );
	f.AddLayoutTextBox( "Owner", "OwnerProp" );
	f.AddLayoutCheckbox( "Barks", "BarksProp" );
}

You also need to make sure the data is valid. Modify the Validate() method like this:

public string Validate( string propertyName )
{
	switch( propertyName )
	{
		case "NameProp":
			if( this.NameProp == "" )
			{
				return "You must give it a name!";
			}
			break;
	}
	return "";	// no errors
}

That’s really all you need to do in the entity class itself! Now you are ready to display the form. Add this code, for example in a button press event:

var dog = new Dog();
var f = new DynForm( "Add Dog", dog );
f.ShowDialog();

The result looks like this (notice that it does not allow you to save the form, since the name field does not validate):

dynform_example

As you can see, with relatively little code you can easily create forms for a multitude of data entities.

Available Controls

DynForm supports many input controls out of the box:

  • Textbox
  • Masked textbox
  • Checkbox
  • Number spinner
  • Date input
  • Labels & headers
  • Dropdown list
  • Listbox

Listboxes are used for more complex situation. Here the user can add and remove items based on a seprate list of available items. Listboxes can additionally have checkboxes, dropdown lists and text fields associated with each item.


Viewing all articles
Browse latest Browse all 79

Trending Articles