Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleLet the user press Ctrl-A to select all of the text in a TextBox in VB .NET
DescriptionThis example shows how to let the user press Ctrl-A to select all of the text in a TextBox in VB .NET.
Keywordsctrl-A, control-A, select text
CategoriesStrings, 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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated