The TextBox control has a single unique event, the TextChanged event, which is fired every time the text on the control is changed, either because the user has typed a character or because of a paste operation. Another event that is quite common in programming the TextBox control is the KeyPress event, which occurs every time a key is pressed and reports the character that was pressed. You can use this event to capture certain keys and modify the program’s behavior depending on the character typed.
Suppose that you want to use the TextEditor application to prepare messages for transmission over a telex line. As you may know, a telex can’t transmit lowercase characters or special symbols. The editor must convert the text to uppercase and replace the special symbols with their equivalent strings: DLR for $, AT for @, O/O for %, BPT for #, and AND for &. You can modify the default behavior of the TextBox control from within the KeyPress event so that it converts these characters as the user types.
By capturing keystrokes, you can process the data as they are entered, in real time. For example, you can make sure that a TextBox accepts only numeric or hexadecimal characters and rejects all others. To implement an editor for preparing text for telex transmission, use the KeyPress event handler shown in Listing 4.7.
Listing 4.7: Handling Keystrokes for a TELEX message
Private Sub txtEditor KeyPress( _
ByVal sender As Object, _
ByVal e As System.Windows. _
Forms.KeyPressEventArgs) _
Handles txtEditor.KeyPress
If System.Char.IsControl(e.KeyChar) Then Exit Sub
Dim ch As Char = Char.ToUpper(e.KeyChar)
Select Case ch.ToString
Case "@"
txtEditor.SelectedText = "AT"
Case "#"
txtEditor.SelectedText = "BPT"
Case "$"
txtEditor.SelectedText = "DLR"
Case "%"
txtEditor.SelectedText = "O/O"
Case "&"
txtEditor.SelectedText = "AND"
Case Else
txtEditor.SelectedText = ch
End Select
e.Handled = True
End Sub
Code language: VB.NET (vbnet)
The very first executable statement in the event handler examines the key that was pressed and exits if it is a special editing key (Delete, Backspace, Ctrl+V, and so on). If so, the handler exits without taking any action. The KeyChar property of the e argument of the KeyPress event reports the key that was pressed. The code converts it to a string and then uses a Case statement to handle individual keystrokes. If the user pressed the $ key, for example, the code displays the characters DLR. If no special character was pressed, the code displays the character pressed as is from within the Case Else clause of the Select statement.
Cancelling Keystrokes
Before you exit the event handler, you must “kill” the original key pressed, so that it won’t appear on the control. You do this by setting the Handled property to True, which tells VB that it shouldn’t process the keystroke any further. If you omit this statement, the special characters will be printed twice: once in their transformed format (DLR$, AT@, and so on) and once as regular characters. You can also set the SuppressKeyPress property to True to cancel a keystroke; the Common Language Runtime (CLR) will not pass the keystroke to the appropriate control.
Capturing Function Keys
Another common feature in text-editing applications is the assignment of special operations to the function keys. The Notepad application, for example, uses the F5 function key to insert the current date at the cursor’s location. You can do the same with the TextEditor application, but you can’t use the KeyPress event — the KeyChar argument doesn’t report function keys. The events that can capture the function keys are the KeyDown and KeyUp events. Also, unlike the KeyPress event, these two events don’t report the character pressed, but instead report the key’s code (a special number that distinguishes each key on the keyboard, also known as the scancode), through the e.KeyCode property.
The keycode is unique for each key, not each character. Lower and uppercase characters have different ASCII values but the same keycode because they are on the same key. For example, the number 4 and the $ symbol have the same keycode because the same key on the keyboard generates both characters. When the key’s code is reported, the KeyDown and KeyUp events also report the state of the Shift, Ctrl, and Alt keys through the e.Shift, e.Alt, and e.Control properties.
The KeyUp event handler shown in Listing 4.8 uses the F5 and F6 function keys to insert the current date and time in the document. It also uses the F7 and F8 keys to insert two predefined strings in the document.
Listing 4.8: KeyUp Event Examples
Private Sub txtEditor KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles txtEditor.KeyUp
Select Case e.KeyCode
Case Keys.F5 :
txtEditor.SelectedText = _
Now().ToLongDateString
Case Keys.F6 :
txtEditor.SelectedText = _
Now().ToLongTimeString
Case Keys.F7 :
txtEditor.SelectedText = _
"MicroWeb Designs, Inc."
Case Keys.F8 :
txtEditor.SelectedText = _
"Another user-supplied string"
End Select
End Sub
Code language: VB.NET (vbnet)
Windows already uses many of the function keys (for example, the F1 key for help), and you shouldn’t modify their original functions. With a little additional effort, you can provide users with a dialog box that lets them assign their own strings to function keys. You’ll probably have to take into consideration the status of the Shift, Control, and Alt properties of the event’s e argument, which report the status of the Shift, Ctrl, and Alt keys, respectively. To find out whether two of the modifier keys are pressed along with a key, use the AND operator with the appropriate properties of the e argument. The following If clause detects the Ctrl and Alt keys:
If e.Control AND e.Alt Then
{ Both Alt and Control keys were down}
End If
Code language: VB.NET (vbnet)