Click here to Skip to main content
15,891,706 members
Articles / Programming Languages / C#
Article

Why Develop Custom Controls? Just Customize Generic Controls. Part 1: Flashing

Rate me:
Please Sign up or sign in to vote.
3.03/5 (15 votes)
19 Feb 2007CPOL3 min read 35.1K   193   35   6
Plug features into generic classes or controls

Introduction

This is the first part of a series of articles discussing how to introduce new features without developing derived classes. In particular, most examples are related to visual Windows components, for improving UI experiences. You might be interested in inbox labels and color picker as combobox in later articles of the series.

Background

For GUI programming, from time to time, you need something more than the standard tool palettes can provide. One obvious solution is to look for third party components commercial or free, especially when the deadline is closing. However, sometimes you just need a little bit extra, and those available might be an overkill or too expensive. For example, you might just need a button with flashing background.

If you decide to implement the visual effect with your own code, a quick solution is to write some event handling function in the form to handle the draw functions/events of the button. If you want to have this button available in other forms or project, you will be thinking of developing a new in-house component of your own derived from the Button class. This is a very conventional approach of introducing new features to a class.

However, if you don't want to deal with design time issues of developing visual components, or you want to introduce this flashing background feature to a few derived classes of the Button class, you might want an alternative solution. Otherwise, you will end up with developing a derived class for each of these derived classes of the Button class, or you have to reconstruct the inheritance structures to make them inherit from the same base class with the flashing feature. I have seen legacy projects with such approaches, especially in those third party component suits.

How about if your client asks you to introduce this fancy flashing background to a textbox, or a combobox? Are you going to develop derived components from the TextBox class and the ComboBox class? Do you think these will make your own custom tool palettes overcrowded?

Unless you are going to introduce tons of new features to a class, it might be overkill or too expensive to develop a derived class for a small feature.

The solution is to plug the feature to existing classes.

Using the Code

The codes were constructed with Visual Studio 2005, and it shouldn't be difficult to copy the code to other C# development environments. This project in the above download link contains two example classes: Flashing and InTextBoxLabel. The second one will be discussed in the next article.

C#
public class Flashing
{
    Control control;
    public Flashing(Control control)
    {
        this.control = control;
        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += new EventHandler(time_Tick);
        timer.Enabled = true;
    }

    void time_Tick(object sender, EventArgs e)
    {
        control.BackColor = Color.FromArgb( control.BackColor.ToArgb() ^  0x031123);
    }

    private Timer timer;
}

In the client codes of a Form, you may plug the Flashing class to generic visual controls, or third party controls as long as they are derived from the generic ones.

C#
private void Form1_Load(object sender, EventArgs e)
{
    new Flashing(button1);
    new Flashing(textBox1);
    new Flashing(comboBox1);
}

When the form is loaded, these controls will be flashing.

In Form1_Load, objects of class Flashing are created without explicit reference. Will they be disposed when exiting the scope of this function? Not at all. Please check the MSDN documents about System.Windows.Forms.Timer and you will know why.

This flashing feature is not likely to be part of your projects, it is just a simple demo illustrating the design pattern.

This is the first of a series of articles discussing developing of half-component. In future articles, I will introduce how to develop an inbox label for compact UI, using the design pattern described above, rather than developing a derived class of ComboBox. I would also introduce a handy UI design called In-Textbox-Label for compact UI in this series.

Points of Interest

As you can see, this design pattern is based on the builder pattern. The Control class is the builder, and the Flashing class is the director. In most cases, the builder provides services to the director, and the director delegates tasks to the builder. In this case, the director provides services to the builder. This design pattern will be extended, as you will see from later articles.

References

License

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


Written By
Software Developer
Australia Australia
I started my IT career in programming on different embedded devices since 1992, such as credit card readers, smart card readers and Palm Pilot.

Since 2000, I have mostly been developing business applications on Windows platforms while also developing some tools for myself and developers around the world, so we developers could focus more on delivering business values rather than repetitive tasks of handling technical details.

Beside technical works, I enjoy reading literatures, playing balls, cooking and gardening.

Comments and Discussions

 
GeneralAbout IDisposable in the 2nd part of the series Pin
Zijian19-Jan-07 20:33
Zijian19-Jan-07 20:33 
Questionsurvive GC ? Pin
Luc Pattyn18-Jan-07 7:17
sitebuilderLuc Pattyn18-Jan-07 7:17 
AnswerRe: survive GC ? Pin
Zijian18-Jan-07 14:11
Zijian18-Jan-07 14:11 
GeneralWhy... Pin
shaul_ahuva18-Jan-07 3:59
shaul_ahuva18-Jan-07 3:59 
GeneralRe: Why... Pin
Zijian18-Jan-07 13:52
Zijian18-Jan-07 13:52 
QuestionRight Pattern? Pin
Adrian Brown17-Jan-07 20:57
Adrian Brown17-Jan-07 20:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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