Pages

Monday, October 13, 2008

Intro

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 xKeys which contains the names of the controls that we will processing. Additionaly

Dictionary xProperties which contains the property of each control

Dictionary 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);

}

Collapse

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.

0 comments: