Click here to Skip to main content
Licence CPOL
First Posted 7 Dec 2006
Views 12,261
Downloads 95
Bookmarked 11 times

Implement a base form for automatic data transfer

By | 7 Dec 2006 | Article
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sarafian

Team Leader
ALGOSYSTEMS
Greece Greece

Member

I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies
 
I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.
 
I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.
 
I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 8 Dec 2006
Article Copyright 2006 by Sarafian
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid