Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
120
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

260 Chapter 6 BASIC WINDOWS CONTROLS

The Undo/Redo Commands

The Undo command (Listing 6.7) is implemented with a call to the Undo method. However, because the Undo method works like a toggle, we must also toggle its caption from Undo to Redo and vice versa, each time the command is activated.

Listing 6.7: The Undo/Redo Command of the Edit Menu

Private Sub EditUndo_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles EditUndo.Click

If EditUndo.Text = “Undo” Then

Editor.Undo()

EditUndo.Text = “Redo”

Else

Editor.Undo()

EditUndo.Text = “Undo”

End If

End Sub

As I mentioned earlier, if you edit the text after an undo operation, you can no longer redo the last undo operation. This means that as soon as the contents of the TextBox control change, the caption of the first command in the Edit menu must become Undo, even it’s Redo at the time. The Redo command is available only after undoing an operation and before editing the text. So, how do we know that the text has been edited? The TextBox control fires the TextChanged event every time its contents change. We’ll use this event to restore the caption of the Undo/Redo command to Undo. Insert the following statements in the TextChanged event of the TextBox control:

Private Sub Editor_TextChanged(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Editor.TextChanged

EditUndo.Text = “Undo”

End Sub

Capturing Keystrokes

The TextBox control has no unique methods or events, but it’s quite common in programming to use this control to capture and process the user’s keystrokes. The KeyPress event occurs every time a key is pressed, and it 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 you want to use the TextPad application (discussed in the preceding sections) 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.

The TELEXPad application is identical to the TextPad application, but customized for preparing telex messages. (Not that the telex is growing in popularity, but there are situations in which

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

THE TEXTBOX CONTROL 261

some custom preprocessing of the data is required.) By capturing keystrokes, you can process the data as they are entered, in real time. For example, you could make sure that numeric values fall within a given range or that hexadecimal digits don’t contain invalid characters, and so on. The only difference is the modified application’s KeyPress event. The KeyPress event handler of the TELEXPad application is shown in Listing 6.8.

Listing 6.8: TELEXPad Application’s KeyPress Event Handler

Public Sub Editor_KeyPress(ByVal sender As Object, _

ByVal e As System.WinForms.KeyPressEventArgs) _ Handles Editor.KeyPress

Dim ch As Char Dim CrLf As String

If System.Char.IsControl(e.KeyChar) Then Exit Sub CrLf = vbCrLf

ch = e.KeyChar.ToChar ch = ch.ToUpper(ch) Select Case ch

Case “@”.ToChar Editor.SelectedText = “AT”

Case “#”.ToChar Editor.SelectedText = “BPT”

Case “$”.ToChar Editor.SelectedText = “DLR”

Case “%”.ToChar Editor.SelectedText = “O/O”

Case “&”.ToChar Editor.SelectedText = “AND”

Case Else Editor.SelectedText = ch

End Select e.Handled = True

End Sub

The very first executable statement in the event handler examines the key that was pressed and exits if it was a special editing key (Del, Backspace, Ctrl+V, and so on). The KeyChar property of the e argument of the KeyPress event reports the key that was pressed. To convert it a character, we call its ToChar method, and in the following line, we convert the character to uppercase by calling the ToUpper method. Normally, you would combine the two statements:

ch = e.KeyChar.ToChar ch = ch.ToUpper(ch)

into one:

ch = System.String.ToUpper(e.KeyChar.ToChar)

but I’ve used a rather verbose syntax to make code more readable.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

262 Chapter 6 BASIC WINDOWS CONTROLS

Then the code 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.

VB6 VB.NET

Before you exit the event handler, you must “kill” the original key pressed, so that it won’t appear on the control. You do by setting the Handled property to True, which tells VB that it shouldn’t process the keystroke any further. In VB6, you could kill a keystroke by setting the KeyAscii argument of the KeyPress event (or the KeyCode argument of the KeyUp event) to zero. The e.KeyChar argument in VB.NET is readonly, and you can’t set it from within your code.

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 TextPad 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 event, which is generated when a key is pressed, and the KeyUp event, which is generated when a key is released. Also, unlike the KeyPress event, KeyDown and KeyUp 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. Lowerand uppercase characters have different ASCII values but the same keycode because they are on the same key. 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.

To program the KeyDown and KeyUp events, you must know the keycode of the key you want to capture. The keycode for the function key F1 is 112 (or the constant Keys.F12), the keycode for F2 is 113 (or the constant Keys.F13), and so on. To capture a special key, such as the F1 function key, and assign a special string to it, program the key’s KeyUp event. The event handler in Listing 6.9 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 6.9: KeyUp Event Examples

Public Sub Editor_KeyUp(ByVal sender As Object, _

ByVal e As System.WinForms.KeyEventArgs) Handles Editor.KeyUp

Select Case e.KeyCode

Case Keys.F5 : editor.SelectedText = Now().ToLongDateString

Case Keys.F6 : editor.SelectedText = Now().ToLongTimeString

Case Keys.F7 : editor.SelectedText = “MicroWeb Designs, Inc.”

Case Keys.F8 : editor.SelectedText = “Another user-supplied string”

End Select

End Sub

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com