) At this point, the text has not
yet appeared in the control.
The TextInput event provides your code with a TextCompositionEventArgs object. This
object includes a Text property that gives you the processed text that??™s about to be received by
the control. Here??™s the code that adds this text to the list shown in Figure 6-6:
Private Sub TextInputPreview(ByVal sender As Object, _
ByVal e As TextCompositionEventArgs)
Dim message As String = "Event: " & e.RoutedEvent.ToString() & _
" " & " Text: " & e.Text
lstMessages.Items.Add(message)
End Sub
Ideally, you??™d use the PreviewTextInput to perform validation in a control like the TextBox.
For example, if you??™re building a numeric-only text box, you could make sure that the current
keystroke isn??™t a letter, and set the Handled flag if it is. Unfortunately, the PreviewTextInput
event doesn??™t fire for some keys that you may need to handle. For example, if you press the
space key in a text box, you??™ll bypass PreviewTextInput altogether. That means you also need
to handle the PreviewKeyDown event.
Unfortunately, it??™s difficult to write robust validation logic in a PreviewKeyDown event
handler because all you have is the Key value, which is a fairly low-level piece of information.
For example, the Key enumeration distinguishes between the numeric key pad and the number
keys that appear just above the letters on a typical keyboard.
Pages:
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340