Click here to Skip to main content
6,910,860 members and growing! (13,652 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Implement A base Form for aytomatic data transfer

By Sarafian

This is a Base Form that Provides a mechanism for a less-code data transfer in and out of it.
C#, Windows, .NETVS2005, Dev
Posted:7 Dec 2006
Views:10,102
Bookmarked:9 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
2 votes for this article.
Popularity: 0.60 Rating: 2.00 out of 5

1
2 votes, 100.0%
2

3

4

5

Introduction

The aim of this library is to create a form to use as a base class for others which handles inword and outwords the data needed to display or get. For example a Login Form.

First of all we need to create the BaseFormData class. This class will allso be used as a 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 processing. Additionaly

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 name Key and set it with Value.

BaseForm is the Base Form class which has a property Data of type BaseFormData. When Setting the Data a function that populates the controls with the corresponding value is called and the reverse happens when the property is Retrieved.

It is usefull for each form that is derived class of BaseFormData is used and creating properies for each value we need and giving defaults values in the contructor.

For 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 the FormLogin
 public partial class FormLogin:BaseForm
 {
  public FormLogin()
  {
   InitializeComponent();
  }
  protected virtual void InitialzeData()
  {
      xData = new FormLoginData();
  }
 }

As you can see InitializeData is overriden to create an instance of the subclassed 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 constuctor 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 technic is that it can be used with all custom controls and for every property they have. And the Form is simple enough to be subclass for every other base form type we might need.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sarafian


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.
Occupation: Team Leader
Company: ALGOSYSTEMS
Location: Greece Greece

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 7 Dec 2006
Editor:
Copyright 2006 by Sarafian
Everything else Copyright © CodeProject, 1999-2010
Web17 | Advertise on the Code Project