Click to See Complete Forum and Search --> : Selecting text in a textbox


Jym
November 28th, 2002, 05:29 PM
in visual basic 6 I used

txtbox.SelStart = 0
txtbox.SelLength = Len(txtbox.Text)

when someone entered a textbox and the text would then become highlighted so the user just had to start typing and the "old" text would be deleted, now in VB .NET when I do that it tells me that .SelStart isn't a system.windows.form.textbox nor is .SelLength is there some way I can have the text highlighted when the user enters the textbox in vb .NET ?

Thanks

Jym

I tried to use
Txtbox.SelectionStart = 0
Txtbox.SelectionLength = Len(Txtbox.Text)

but it doesn't seem to work in my program

Athley
November 29th, 2002, 02:02 AM
It probably works behind the scenes for you. The problem have to be that the Textbox does not get focus..... Try

TextBox1.Focus()

/Leyan

Jym
November 29th, 2002, 09:49 AM
I can't get it to work :(

I even made a little test program with just 2 textboxes on it, and when I go from one to the other with the mouse, they won't highlight the text in the textbox

Athley
November 29th, 2002, 10:08 AM
I made a form with 2 textboxes and a button. I might for example want to check if the first textbox has a numeric value. If not I want to select all text in the textbox. This then worked for me....

Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

If Not IsNumeric(TextBox1.Text) Then
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Strings.Len(TextBox1.Text)
e.Cancel = True
End If

End Sub

The "e.Cancel = True" automatically gives the textbox focus, but if you dont use this in a validating purpose and you for some reason dont get focus for the textbox, then do this.....

TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Strings.Len(TextBox1.Text)
TextBox1.Focus()

I has to work.... :) .... does for me.

/Leyan

Jym
November 29th, 2002, 10:30 AM
I changed the event from textbox1.enter to textbox1.click and now the code works just fine. Don't know why it worked but I will go with what works :)

Jym

Athley
November 29th, 2002, 10:46 AM
Well as I couldn't get it not to work that is good. :D

I had no more tips to give...

/Leyan