Click here to Skip to main content
12,955,372 members (58,516 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 17 Jan 2007

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

, 19 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Plug features into generic classes or controls


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.


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.

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.

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.



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


About the Author

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.

You may also be interested in...


Comments and Discussions

GeneralAbout IDisposable in the 2nd part of the series Pin
Zijian19-Jan-07 20:33
memberZijian19-Jan-07 20:33 
Questionsurvive GC ? Pin
Luc Pattyn18-Jan-07 7:17
memberLuc Pattyn18-Jan-07 7:17 
AnswerRe: survive GC ? Pin
Zijian18-Jan-07 14:11
memberZijian18-Jan-07 14:11 
You are right thinking of GC. The timer is owned by the host form, I guess, in msdn, there is such description:
This timer is optimized for use in Windows Forms applications and must be used in a window.

The host form has reference to timer, and the timer is contained in class Flash, so the host for has reference to class Flash. So, the Flash object can stay there even if there's no explicit reference to it. When you run the demo program, you will see.

However, the implementation of this class in not sound enough because of IDiposable.
Please read the 2nd part for this series.


GeneralWhy... Pin
shaul_ahuva18-Jan-07 3:59
membershaul_ahuva18-Jan-07 3:59 
GeneralRe: Why... Pin
Zijian18-Jan-07 13:52
memberZijian18-Jan-07 13:52 
QuestionRight Pattern? Pin
Adrian Brown17-Jan-07 20:57
memberAdrian 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 19 Feb 2007
Article Copyright 2007 by Zijian
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid