65.9K
CodeProject is changing. Read more.
Home

Implement a base form for automatic data transfer

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (2 votes)

Dec 8, 2006

CPOL

2 min read

viewsIcon

22314

downloadIcon

176

This is a base form that provides a mechanism for a less-code data transfer.

Introduction

The aim of this library is to create a form to use as the base class for others, which handles inward and outward data needed to be displayed or got. For example, a login form.

First of all, we need to create the BaseFormData class. This class will also be used as the base class for the implementation of the final classes.

BaseFormData defines three generic objects. A List<string> xKeys which contains the names of the controls that we will be processing. Additionally:

  • Dictionary<string, string> xProperties which contains the property of each control.
  • Dictionary<string, object> xValues which contains the value of the property of each control.

A set of help functions is defined in order to make the accessibility of the above logic easier.

The most basic one is bool AddData(string Key,string PropertyName,object Value) which tells the object that it will be handling the property named PropertyName of the control with the name Key and set it with Value.

BaseForm is the base Form class which has a property Data of type BaseFormData. When setting data, a function that populates the controls with the corresponding value is called, and the reverse happens when a property is retrieved.

Using the code

As an example, assume we need to build a login form. First, we create the derived class of BaseFormData:

public class FormLoginData:MyComponents.CustomControls.BaseForms.BaseFormData
{
    public FormLoginData()
    {
        AddData(UserNameKey, "Text", "");
        AddData(PasswordKey, "Text", "");
        AddData(RememberKey, "Checked",false);
    }

    protected const string UserNameKey="tbrUserName";
    protected const string PasswordKey="tbrPassword";
    protected const string RememberKey = "cbRemember";
    public string UserName
    {
        get
        {
            return (string)GetValue(UserNameKey);
        }
        set
        {
            SetValue(UserNameKey, value);
        }
    }
    public string Password
    {
        get
        {
            return (string)GetValue(PasswordKey);
        }
        set
        {
            SetValue(PasswordKey, value);
        }
    }
    public bool Remember
    {
        get
        {
            return (bool)GetValue(RememberKey);
        }
        set
        {
            SetValue(RememberKey, value);
        }
    }
}

and FormLogin:

public partial class FormLogin:BaseForm
{
    public FormLogin()
    {
        InitializeComponent();
    }
    protected virtual void InitialzeData()
    {
        xData = new FormLoginData();
    }
}

As you can see, InitializeData is overridden to create an instance of the sub-classed BaseFormData that we will use.

Three objects must be present in the form:

  • TextBox tbrUserName
  • TextBox tbrPassword
  • CheckBox cbRemember

In order to use them, we define the constructor as:

public FormLoginData()
{
    AddData(UserNameKey, "Text", "");
    AddData(PasswordKey, "Text", "");
    AddData(RememberKey, "Checked",false);
}

using the following consts:

protected const string UserNameKey="tbrUserName";
protected const string PasswordKey="tbrPassword";
protected const string RememberKey = "cbRemember";

Finally, using the form would be like this:

MyComponents.CustomControls.Forms.FormLogin xForm = 
    new MyComponents.CustomControls.Forms.FormLogin();
MyComponents.CustomControls.Forms.FormLoginData xTemp = 
   (MyComponents.CustomControls.Forms.FormLoginData)xForm.Data;

//If we wish to assign values
xTemp.UserName = "UN";
xTemp.Password = "PW";
xTemp.Remember = false;
xForm.Data = xTemp;
if (xForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
    return false;
MyComponents.CustomControls.Forms.FormLoginData xLogin = 
   (MyComponents.CustomControls.Forms.FormLoginData)xForm.Data;
LoginUserName=xLogin.UserName;
LoginPassword=xLogin.Password;
if(xLogin.Remember)
    SaveLogin();

The good thing about this technique is that it can be used with all custom controls and for every property they have. And the Form is simple enough to be sub-classed for every other base form type we might need.