CodeItBetter Programming Another VB Programming Blog

How to select all text with Ctrl+A in all TextBoxes in a form

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'Controls - How to select all text with Ctrl+A in all TextBoxes in a form
'Set KeyPreview = True. Then in the form's KeyPress event handler, check for
'the Ctrl+A key combination. If the user presses Ctrl-A, see if the active
'control is a TextBox. If it is, use its SelStart and SelLength properties to
'select all of its text.

Private Sub Form_Load()
    KeyPreview = True
End Sub
 
Private Sub Form_KeyPress(KeyAscii As Integer)
    Const ASC_CTRL_A As Integer = 1
    Dim txt As TextBox
 
    ' See if this is Ctrl+A.
    If KeyAscii = ASC_CTRL_A Then
        ' The user is pressing Ctrl+A. See if the
        ' active control is a TextBox.
        If TypeOf ActiveControl Is TextBox Then
            ' Select the text in this control.
            Set txt = ActiveControl
            txt.SelStart = 0
            txt.SelLength = Len(txt.Text)
        End If
    End If
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.