
Originally Posted by
Karl77
Textbox problem
In my program I have a routine that selects all text in a textbox when it gets the focus (optional).
This doesn't work with TextBoxW so very well.
Code:
Private Sub TextBoxW1_GotFocus()
With TextBoxW1
.SelStart = 0
.SelLength = 999
End With
End Sub
The effect is, that the text gets selected from the start to the clicked point.
The intrinsic textbox doesn't show this behavior.
The text gets completely selected as intended.
I did a test with an intrinsic VB TextBox and the TextBoxW:
Code:
Private Sub Text1_GotFocus()
With Text1
.SelStart = 0
.SelLength = 999
End With
End Sub
Private Sub TextBoxW2_GotFocus()
With TextBoxW2
.SelStart = 0
.SelLength = 999
End With
End Sub
Both do select the whole text at GotFocus but the mouse click changes the length of selection to actual clicked point.
So in my point behavior is exactly the same.
EDIT:
Proper implementation of AutoSelect by OnFocus would be as following:
Code:
Private Sub TextBoxW2_GotFocus()
If GetMouseStateFromMsg() = 0 Then
With TextBoxW2
.SelStart = 0
.SelLength = Len(.Text)
.Tag = "Focused"
End With
End If
End Sub
Private Sub TextBoxW2_LostFocus()
TextBoxW2.Tag = vbNullString
End Sub
Private Sub TextBoxW2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
With TextBoxW2
If .SelLength = 0 And .Tag = vbNullString Then
.Tag = "Focused"
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Sub
EDIT2:
I could include an "AutoSelectOnFocus" property which would take care of this internally.