locked
Windows Textbox property “SelectionLength” is not working in .Net 2.0 RRS feed

  • Pergunta

  • 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.

    terça-feira, 5 de junho de 2007 13:03

Respostas

  • 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.


    terça-feira, 5 de junho de 2007 14:11

Todas as Respostas

  • 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.


    terça-feira, 5 de junho de 2007 14:11
  •  

    Hello Dinesh,

    Try this

     

    Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter 

        'TextBox1.Focus()

        TextBox1.Select(0, TextBox1.TextLength - 1)

    End Sub

     

    Simão

    landsimao.hotmail.com

    http://weblogs.pontonetpt.com/landsimao

    terça-feira, 5 de junho de 2007 14:23
  • Hi

     

    Its working!!!!!!!!! thanks a lot...

     

    Regards,

    Dinesh

    quarta-feira, 6 de junho de 2007 05:31
  • woooow great resolution its working Thanks a lot ..god bless you

    segunda-feira, 2 de junho de 2008 13:25
  • Keep it simple:

     

    Code Snippet

    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click

    If 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

     

     

    Notice also that there is a SelectAll() method.
    segunda-feira, 2 de junho de 2008 14:59
  • 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.

     

     

     


    Tong
    sexta-feira, 27 de maio de 2011 03:37
  • The TextBox.SelectAll() method does not work from within a KeyDown event :(
    quinta-feira, 9 de abril de 2020 15:49