Highlighting Text in a Textbox

mwrob

New member
Joined
Apr 11, 2007
Messages
1
Programming Experience
1-3
I had a simple function written in VB 6 for selecting all the text in a textbox when it got focus. Does anyone know how to to the same in VB 2005.

Public Sub Highlight()

With Screen.ActiveForm
If (TypeOf .ActiveControl Is TextBox) Then
.ActiveControl.SelStart = 0
.ActiveControl.SelLength = Len(.ActiveControl)
End If
End With

End Sub

The SelStart and SelLength methods don't appear to exist in VB.Net.
 
VB.NET:
Public Function VerifyInput(ByRef InputCharacterAscii As Short) As Short
Dim ThisControl As New System.Windows.Forms.TextBox
Dim StartPosition_Renamed_int As Integer
Dim NetSelLength_int As Integer
Dim MyActiveForm As Form = System.Windows.Forms.Form.ActiveForm
 
ThisControl = MyActiveForm.ActiveControl 
ThisControl.Text = MyActiveForm.ActiveControl.Text
StartPosition_Renamed_int = ThisControl.SelectionStart
NetSelLength_int = ThisControl.SelectionLength

This is some code I dug up..maybe it will help you.
 
To select all text when textbox is entered use the Enter event and the SelectAll method:
VB.NET:
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
    TextBox1.SelectAll()
End Sub
 
Back
Top