Tuesday, July 24, 2012

Designing resuable control

Before we start the coding in any project, we have to identify the reusable screen, part of the screen or a group of fields. In this stage, we are developing custom control to be reuse in one or more projects.

Below is the reasons of developing custom control:

  • To reduce the overall development time - in Visual Studio, the custom control can be drag from the Toolbox and drop onto any WinForm or UserControl. Note: the custom control that you have added to the project will appear in the Toolbox after you have compiled the project or solution (if your custom control is residing in other project).
  • To reduce the testing time - this requires test once only because the control is reusable.
Below is the situation where you need to design a reusable control:
  • You are working on system that requires to capture "record status" which is used in many screens. 
  • The user might request for enhancement on this input control but the enhancement is not an urgent priority now
  • You are splitting the system into multiple small section to be handle by different programmer.
For example, in your system, there is a field that occur in many screens and it is called "status". It contains 4 statuses.In this case, you may add a new Class into the project and then set the base class (or parent class) to "ComboBox". It has a method name RefreshData which is responsible for loading the available statuses (which could be from database and it's up-to your design).
public class StatusComboBox : ComboBox
{
    public void RefreshData()
    {
        this.Items.Clear();
        this.Items.Add("Pending");
        this.Items.Add("In Progress");
        this.Items.Add("Completed");
        this.Items.Add("Cancelled");
    }
} 
After you have compiled the project, the StatusComboBox custom control will appear in the Toolbox. Below is the screen snapshot of the Toolbox:



Now, you may drag this control onto the Form and call "statusComboBox1.RefreshData()" to load the items.

No comments:

Post a Comment