Neither of the two code pieces are working for me...
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocusTextBox1.SelectionStart = 0
TextBox1.SelectionLength = Len(TextBox1.Text)
End Sub-= OR =-
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocusTextBox1.SelectAll()
End Sub
When i put text in the textbox, move focus away, and then back again, i would expect the text to be selected, however it is not working for me. Any ideas (Visual Basic 2005 Professional)

textbox selectall not working
Gerardicus
You could just do it once then save it in a class library which you can use in any project without having to type it all every time.
Re the control arrays - have a look at a question I raised a few weeks back
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=222711&SiteID=1
This was about a similar annoyance (which didn't really have an answer, only a workaround) but the code in there may point you in the right direction to using control arrays in VB net.
Dave
crazy jared
Did you see the previous post - I did two in quick sequence and your response indicates you may have only read the last one
Dave
Frank Green
So I guess the intial question is still unanswered, and I'm still wondering why the following code doesn't work as it would in VB 6.0 and is there a simple workaround
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.SelectAll()
End Sub
(works with tab key, but when the user uses the mouse to change fields, the gotfocus event fires but does not allow the text to be selected)
Tim Daplyn
I have come up with the following and it seems to work
(EDIT NOTE: THIS CODE HAS A SMALL PROBLEM, SEE REVISED CODE BELOW IN NEXT MESSAGE)
Public CurrentTextBoxControl As Control Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf (ctrl) Is TextBox Then AddHandler ctrl.Click, AddressOf AnyTextBox_Click End If Next End SubPrivate Sub AnyTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomerIDTextBox.Click
TextBoxFocusHandler(sender, CurrentTextBoxControl)
End Sub Private Sub TextBoxFocusHandler(ByVal sender As System.Windows.Forms.TextBox, ByRef CurrentTextBoxControl As Control) If Not sender Is CurrentTextBoxControl Then ' current control has changed so we'll select the textsender.SelectAll ()
End IfCurrentTextBoxControl = sender
End SubNow I just have to figure out how to put it in a class (and that class) in my various forms like you suggested.
kppraki
I noticed the same thing
Girish K
Set the textbox's hideselection property to false so the text will be selected when the textbox does not have focus.
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemwindowsformstextboxbaseclasshideselectiontopic.asp
GlenW
You're quite right - silly suggestion.
However if you set a flag when clicking on the box and clear it when the box loses focus and only select the text when the flag is not set it all works as you expect.
The Help file suggests that rather than use the GetFocus and LoseFocus the Enter and Leave events are used. However these seem to suffer from the same problem.
As you say all this worked "properly" in VB6 and in VB5 which was my last experience before obtaining VB Express.
Dave
Tara Nerurkar
Put the same code into the click event
Dave
Peter Fitzgibbons
I've just done a simple experiment with a form with two textboxes on it.
With the following code in box1
My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)TextBox1.SelectionStart = 0
TextBox1.SelectionLength = Len(TextBox1.Text)
Put some text in box1 so you can see when it is highlighted and then tab between the two boxes. When box 1 is selected there is a beep and the text is highlighted.However when box 2 is selected and you then click on box 1 you still get the beep but the text is not selected. Which suggests that something is deselecting the text after it gets selected.
Dave
Dmagueur
Yes, i noticed the first post, sorry to forget to acknowledge it
I'm working on converting to vb 6.0 method (see code clip above) to something that is vb 2005 compatible, I'm just really slow with VB 2005
, man i could fly with 6.0, but it's time i brought myself into the 21st century, lol
Chaitanya Tyagi
Thanks for the suggestion but that has the desired effect when clicking into the text box from another field, but with the following side effect...
sometimes users will use their mouse to , say for example, move to another spot within the text to do some editing, and we don't want to select all then, only when our text box initially gets focus. This code worked fine in vb 6.0
Saeed Ghadiri
Please note: there is a problem with the above code, in that tabbing into a different field doesn't change the CurrentTextBoxControl value, so if you tab to change focus, and then click back on the previous control using your mouse, the text won't be selected.
The below code works much better.
Public FocusChange As Boolean Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Me.Controls If TypeOf (ctrl) Is TextBox Then Dim xctrl As System.Windows.Forms.TextBox = ctrlxctrl.SelectAll()
AddHandler ctrl.Click, AddressOf AnyTextBox_Click AddHandler ctrl.GotFocus, AddressOf AnyTextBox_GotFocus End If Next End Sub Private Sub AnyTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) If FocusChange Then ' current textbox has changed so we'll select the textsender.SelectAll()
FocusChange =
False End If End Sub Private Sub AnyTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)FocusChange =
True End SubKeith Swem
I could do that, but I have a lot of text boxes, and a lot of forms, and I'm in the habit of configuration this functionality to all text boxes I create, so i'd probably be looking at a lot of extra work, and I may just have to do that...
but this leads to another question... In VB6 i could do something like this (using a text box control array)
Private Sub Text1_Click(Index As Integer)
If Index <> CurrentTextBoxControl Then
' current control has changed so we'll select the text
Text1(Index).SelStart = 0
Text1(Index).SelLength = Len(Text1(Index).Text)
End If
CurrentTextBoxControl = Index
End Sub
How can VB.net 2005 do this
Albert_Khor
I don't need the text to be selected when i leave the field. I need the text to be selected when i enter the field.
I did some further testing. My code actually works if i tab into the field, but if i click into the field it doesn't select the text. Is there a workaround