Answered by:
Windows Textbox property “SelectionLength” is not working in .Net 2.0

Question
-
I am migrating following VB6.0 code to .Net 2.0 (Windows application).
VB6.0 Code :-
Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
In .Net 2.0 the migrated code is as followed :-
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
TextBox1.SelectionStart = 0
TextBox1.SelectionLength = TextBox1.Text.Length
End Sub
But the above converted code is not working properly in .Net 2.0. In VB6.0 on click of Text1 control, the text inside the Text1 is selected and highlighted. But when I migrated the code to .Net 2.0 , on click of TextBox1 control the text in TextBox1 control is not highlighted. However if we use Tab then its working fine in .Net 2.0.
Kindly suggest what should I do to highlight the text in .Net 2.0 on the click event.
Tuesday, June 5, 2007 1:03 PM
Answers
-
Tricky problem, the TextBox receives the MouseDown event after Enter runs and that event will put the caret at the clicked text location. If you look real close, you can just barely see the selection highlight flash on and back off. Preventing that MouseDown event from reaching the control is not practical, Enter may also run when you tab into the control.
A workaround is to do the selection after all the mouse message handling is done. Windows Forms makes that easy with BeginInvoke():
Public Class Form1
Private Delegate Sub DelaySelectDelegate(ByVal box As TextBox)
Private Sub TextBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
Me.BeginInvoke(New DelaySelectDelegate(AddressOf DelaySelect), TextBox2)
End Sub
Private Sub DelaySelect(ByVal box As TextBox)
box.SelectionStart = 0
box.SelectionLength = box.Text.Length
End Sub
End Class
You probably want to put this code in a derived text box class so you don't have to repeat it for every text box on your form.Tuesday, June 5, 2007 2:11 PM
All replies
-
Tricky problem, the TextBox receives the MouseDown event after Enter runs and that event will put the caret at the clicked text location. If you look real close, you can just barely see the selection highlight flash on and back off. Preventing that MouseDown event from reaching the control is not practical, Enter may also run when you tab into the control.
A workaround is to do the selection after all the mouse message handling is done. Windows Forms makes that easy with BeginInvoke():
Public Class Form1
Private Delegate Sub DelaySelectDelegate(ByVal box As TextBox)
Private Sub TextBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
Me.BeginInvoke(New DelaySelectDelegate(AddressOf DelaySelect), TextBox2)
End Sub
Private Sub DelaySelect(ByVal box As TextBox)
box.SelectionStart = 0
box.SelectionLength = box.Text.Length
End Sub
End Class
You probably want to put this code in a derived text box class so you don't have to repeat it for every text box on your form.Tuesday, June 5, 2007 2:11 PM -
Hello Dinesh,
Try this
'TextBox1.Focus()
TextBox1.Select(0, TextBox1.TextLength - 1) End SubSimão
landsimao.hotmail.com
Tuesday, June 5, 2007 2:23 PM -
Hi
Its working!!!!!!!!! thanks a lot...
Regards,
Dinesh
Wednesday, June 6, 2007 5:31 AM -
woooow great resolution its working Thanks a lot ..god bless you
Monday, June 2, 2008 1:25 PM -
Keep it simple:
Code SnippetPrivate
Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.ClickIf Me.TextBox1.SelectedText = "" Then Me.TextBox1.SelectAll()
End Sub
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
Me.TextBox1.SelectAll()
End Sub
Monday, June 2, 2008 2:59 PM -
Jaystation have the right answer. I have the same problem. but actually the problem is the code convert wrongly to .net Vb6 use GotFocus, .net use be GotFocus as well.
TongFriday, May 27, 2011 3:37 AM -
The TextBox.SelectAll() method does not work from within a KeyDown event :(Thursday, April 9, 2020 3:49 PM