select all text in a textbox or rich text box when user hits CTRL-A ?

  • Thread starter Thread starter Mad Scientist Jr
  • Start date Start date
M

Mad Scientist Jr

how do you select all text in a textbox or rich text box when the user
presses CTRL-A ?

i am looking to recreate the vb 6 code below for vb.net

thanks.


Private Sub MyTextbox_KeyDown(KeyCode As Integer, Shift As Integer)
Dim bShiftDown As Boolean
Dim bCtrlDown As Boolean
Dim bAltDown As Boolean
bShiftDown = (Shift And vbShiftMask) > 0
bCtrlDown = (Shift And vbCtrlMask) > 0
bAltDown = (Shift And vbAltMask) > 0
If (KeyCode = Asc("a") Or KeyCode = Asc("A")) _
And _
(bCtrlDown And (Not (bShiftDown Or bAltDown))) _
Then
MyTextbox.SelStart = 0
MyTextbox.SelLength = Len(MyTextbox.Text)
End If
End Sub
 
In the KeyDown event of the TextBox place this code;

If e.Control And e.KeyValue = 65 Then
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = TextBox1.Text.Trim.Length
End If

To keep the Beep from firing, in the KeyPress event place this code:

e.Handled = True


HTH
Les
See articles, free code, add-ins, books, general .NET articles at
http://www.knowdotnet.com
 
I figured this out. Just replace txtOut with the name of your
textbox...

Private Sub txtOut_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtOut.KeyDown
If e.Control = True And e.KeyCode = Keys.A Then
txtOut.SelectionStart = 0
txtOut.SelectionLength = Len(txtOut.Text)
End If
End Sub
 
Back
Top