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:
-
Control
ControlToFlash: This is the control you want to flash upon a certain condition (Similar to the RequiredFieldValidator ControlToValidate property in ASP.NET)
-
int
NumberOfFlashes: How many flash cycles.
-
int
FlashCycleDuration: Duration in milliseconds between background color changes (to achieve the flashing effect)
-
Color
FlashColor: The alternating color during the flash cycle.
-
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