|
|
Title | Let the user press Ctrl-A to select all of the text in a TextBox in VB .NET |
Description | This example shows how to let the user press Ctrl-A to select all of the text in a TextBox in VB .NET. |
Keywords | ctrl-A, control-A, select text |
Categories | Strings, Controls, VB.NET |
|
|
When the TextBox's KeyPress event sees the Ctrl-A key code (1), it casts the event's sender into a TextBox and calls the TextBox's SelectAll method. The code then sets e.Handled to True to indicate that the character has been handled. This prevents the TextBox from beeping.
Note that this code does not directly refer to TextBox2 so you can copy and paste the exact same code into any TextBox's KeyPress event handler if you want it to handle Ctrl-A.
|
|
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal _
e As System.Windows.Forms.KeyPressEventArgs) Handles _
TextBox2.KeyPress
If e.KeyChar = Convert.ToChar(1) Then
DirectCast(sender, TextBox).SelectAll()
e.Handled = True
End If
End Sub
|
|
|
|
|
|