Table of Contents
The symbol
returns the reader to the top of the Table of Contents.
Introduction
This article presents methods to
- Insure that a TextBox contains only decimal digits.
- Advances the focus to the next input control when the current Textbox
is filled.
Background
During a recent project developing a Work Order system for both
desktop and mobile use, I needed methods to process TextBox objects in
a special way. Certain TextBoxes could only contain decimal digits.
Other Textboxes were interrelated.
In the figure to the left, four TextBoxes are located contiguously:
ZIP code, telephone area code, telephone prefix, and telephone number.
Each TextBox should accept only decimal digits. And, when the proper
number of digits is entered, focus should be moved to the next
control.
For example, the cursor should be moved to
- the telephone area code TextBox when the ZIP code TextBox contains
five digits.
- the telephone prefix TextBox when the telephone area code TextBox
contains three digits.
- the telephone number TextBox when the telephone prefix TextBox
contains three digits.
- the next Form control when the telephone number TextBox contains four
digits.
Note that advancing from one TextBox to the next is controlled by the
MaxLength property of each TextBox.
Implementation
Insuring Decimal Digits
Insuring that only decimal digits are entered into a TextBox is
accomplished by handling the TextBox KeyDown event. I named the
TextBox KeyDown event handler
test_for_decimal_digits and
it is attached to each TextBox for which decimal digit testing is
desired.
void test_for_decimal_digits ( object sender,
KeyEventArgs e )
{
char current_key = ( char ) e.KeyCode;
bool modifier = e.Control || e.Alt || e.Shift;
bool non_numeric = char.IsLetter ( current_key ) ||
char.IsSymbol ( current_key ) ||
char.IsWhiteSpace ( current_key ) ||
char.IsPunctuation ( current_key );
switch ( e.KeyCode )
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
case Keys.PageUp:
case Keys.PageDown:
case Keys.Delete:
e.SuppressKeyPress = false;
return;
default:
break;
}
if ( !modifier && non_numeric )
{
if ( ( e.KeyCode < Keys.NumPad0 ) ||
( e.KeyCode > Keys.NumPad9 ) )
{
e.SuppressKeyPress = true;
}
}
if ( e.Control && ( e.KeyCode == Keys.V ) )
{
string pasted_text = Clipboard.GetText ( );
string stripped_text = String.Empty;
for ( int i = 0; ( i < pasted_text.Length ); i++ )
{
if ( char.IsDigit ( pasted_text [ i ] ) )
{
stripped_text += pasted_text [ i ];
}
}
if ( !stripped_text.Equals ( pasted_text ) )
{
e.SuppressKeyPress = true;
}
else
{
e.SuppressKeyPress = false;
}
}
}
Auto Advancing through Fields
Advancing from one TextBox to the next is accomplished by handling the
TextBox TextChanged event. I named the TextBox TextChanged event
handler skip_to_next_control
and as before it is attached to each TextBox for which automatic
skipping out is desired.
void skip_to_next_control ( object sender,
EventArgs e )
{
TextBox text_box = ( TextBox ) sender;
int length = 0;
string original_text = text_box.Text;
string trimmed_text = original_text.Trim ( );
if ( original_text.Length != trimmed_text.Length )
{
text_box.Text = trimmed_text;
}
length = text_box.Text.Length;
if ( length == text_box.MaxLength )
{
Control parent = text_box.Parent;
parent.SelectNextControl ( ActiveControl,
true,
true,
true,
true );
}
}
There are some conditions levied against the Form in order to obtain
TextBox advancing.
- The MaxLength of each participating TextBox must be set to the number
of characters that, once reached, will cause the focus to be moved to
the next control. For example, a ZIP code contains 5 digits, so the
MaxLength of the ZIP code TextBox should be set to 5. Likewise, the
telephone area code and prefix should have a MaxLength of 3.
- The TabIndex for contiguous TextBoxes must be monotonically
increasing. The Form Tab Order can be set by clicking on View→Tab
Order in the Visual Studio Designer menu.
When these conditions are met, automatically advancing from one
TextBox to the next can occur.
Attaching Handlers to Multiple Objects
In the Work Order Customer TabPage there are seven TextBoxes to which
both event handlers are to be attached. In Visual Studio, it is
relatively easy to attach event handlers to multiple objects. In the
following figure, the seven TextBoxes are filled with red. I
suggest the following procedure when ever it is necessary to attach
handlers to more than one TextBoxes:
- Make sure that the event handlers you want to attach are in the code
behind and that the code behind has compiled without error.
- Open the Visual Studio Designer.
- Holding down the control key, select all of the TextBoxes to which
event handlers will be attached.
- At the top of Properties, click Events (the small lightening bolt
button). This will open a list of events.
- Click on an event name. Move the cursor to the far right of the event
row. Left click and a dropdown list of event handlers found in the
code behind will appear.
- Choose the event handler appropriate to the event.
- Repeat these steps for each event handler to be attached.
Downloads
No downloads have been included in this article. The Copy Code link
above each source code fragment will copy the code onto the Clipboard.
In 1964, I was in the US Coast Guard when I wrote
my first program. It was written in RPG (note no suffixing numbers). Programs and data were entered using punched cards. Turnaround was about 3 hours. So much for the "good old days!"
In 1970, when assigned to Washington DC, I started my MS in Mechanical Engineering. I specialized in Transportation. Untold hours in statistical theory and practice were required, forcing me to use the university computer and learn the FORTRAN language, still using punched cards!
In 1973, I was employed by the Norfolk VA Police Department as a crime analyst for the
High Intensity Target program. There, I was still using punched cards!
In 1973, I joined Computer Sciences Corporation (CSC). There, for the first time, I was introduced to a terminal with the ability to edit, compile, link, and test my programs on-line. CSC also gave me the opportunity to discuss technical issues with some of the brightest minds I've encountered during my career.
In 1975, I moved to San Diego to head up an IR&D project,
BIODAB. I returned to school (UCSD) and took up Software Engineering at the graduate level. After BIODAB, I headed up a team that fixed a
stalled project. I then headed up one of the two most satisfying projects of my career, the
Automated Flight Operations Center at Ft. Irwin, CA.
I left Anteon Corporation (the successor to CSC on a major contract) and moved to Pensacola, FL. For a small company I built their firewall, given free to the company's customers. An opportunity to build an
air traffic controller trainer arose. This was the other most satisfying project of my career.
Today, I consider myself capable.