Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Fun with C# Timers… Flash a Windows Control to draw attention to it's invalid data

 Download

One common Windows Application development task is to validate a TextBox or a Windows Control against a certain value, or whether the user has entered a value at all before hitting the submit/Ok button, then display some kind of an error.

The most common GUI technique is to display the error using a MessageBox.Show popup after validating the control, with something like "First Name field is empty! Please enter your First Name". I find these popup boxes in a Windows Application to be annoying... especially when combined with the "bump" wav sound associated with them! So to give my users a friendly app, I thought it would be nice to draw the user's attention to the offending control causing the error, rather than having their attention be on the MessageBox popup with the bump wav sound.

In ASP.NET for example, there are visual validators that you can add to your Web form to indicate missing values in text boxes, or missing check in a check box collection, so why not have a similar thing for a Windows Applications? That's where the "ControlFlasher" object comes in handy

See a Demo app using the ControlFlasher object. (You will need .NET 2.0 installed to run this demo)

How to use the ControlFlasher:

Say you have a Windows Form which Collects First Name, Last Name, Age & Zodiac Sign. You want to ensure when the user clicks on the Submit button that all four fields have been filled, and if not, flash the offending TextBox control to draw user's attention to it: Here is a typical use for the class object:

int iNumberOfFlashes = 6;

int iFlashCycleDuration = 100;

Color flashColor = Color.Red;

if (txtbxFirstName.Text.Trim() == "" )

{

    ControlFlasher .Start(txtbxFirstName, iNumberOfFlashes,

       iFlashCycleDuration, flashColor,

       "Please enter first name" );

}

And the txtbxFirstName control would flash.

There are two ways to use the ControlFlasher:

1. By calling the static factory method:

public static ControlFlasher Start(Control controlToFlash,

   int numberOfFlashes,

   int flashCycleDuration,

   Color flashColor,

   string strMessage);

2. By instantiating an object using the constructor:

public ControlFlasher(Control controlToFlash,

   int numberOfFlashes,

   int flashCycleDuration,

   Color flashColor,

   string strMessage);

The ControlFlasher has the following public properties that need to be set:

  1. Control ControlToFlash: This is the control you want to flash upon a certain condition (Similar to the RequiredFieldValidator ControlToValidate property in ASP.NET)
  2. int NumberOfFlashes: How many flash cycles.
  3. int FlashCycleDuration: Duration in milliseconds between background color changes (to achieve the flashing effect)
  4. Color FlashColor: The alternating color during the flash cycle.
  5. string Msg: Any text message you'd like to display in the Text property of the control being flashed during the entire flash period.

GroupBox Example:

Here is an example operating on a Group Box full of check boxes, none of which got selected by the user:

if (!checkBox1.Checked && !checkBox2.Checked && !checkBox3.Checked && !checkBox4.Checked

    && !checkBox5.Checked && !checkBox6.Checked)

{

    ControlFlasher.Start(groupBox1, iNumberOfFlashes,

       iFlashCycleDuration, flashColor,

       "Select at least One Hobby!");

}

How does it work?

Once you start the ControlFlasher object, it would start a timer, the timer would alternate the background color of the ControlToFlash as many times as required, waiting for the given period (in milliseconds) between each color change. Once the number of flashes is finished, the timer would stop itself, and the Control properties are restored back to the original. 

And that's it... say good bye to the annoying MessageBox popups!

Source Code

Here is the implementation:

Download Source

kick it on DotNetKicks.com

Feedback

Posted on 2/9/2011 5:18:32 AM

thanks

Posted on 5/14/2008 5:52:22 PM

Hi pi,

Yes that would be a good suggestion. In fact you can pretty much control how to design the notification or how to grab user attention in many ways.

Feel free to implement your idea then post it here :-)

Yousef

Posted on 5/14/2008 4:55:13 PM

why stop would be usefull:

let all neccesary controls blink, but stop
blinking on the control where the user set
focus on.
all other controls continue blinking.

Posted on 5/14/2008 4:13:50 PM

thats exactly what i have been looking for
and it works nice.
one thing is missing:

a simple stop blinking method when user longer
duration.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author