dcsimg
Results 1 to 2 of 2

Thread: TextBox selection problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2011
    Posts
    193

    TextBox selection problem

    I'm developing an app for Windows Phone 8.1 and having problem to select text correctly. There is a MultiLine TextBox when i search for word, i highlight it, the problem is that, highlighting works perfect if the word is in first line but if it is in the second line, the highlight shifted to left one character and if it is in the third line, the highlight shifted to left two characters
    EX:
    if TextBox text is
    Code:
    aaa-aaa-
    aaa-aaa-
    aaa-aaa-
    and search for "aaa", tapping on Find Next button produce the following highlighting sequence

    Code:
    aaa-aaa-
    aaa-aaa-
    aaa-aaa-
    Code:
    aaa-aaa-
    aaa-aaa-
    aaa-aaa-
    Code:
    aaa-aaa-
    aaa-aaa-
    aaa-aaa-
    Code:
    aaa-aaa-
    aaa-aaa-
    aaa-aaa-
    As you can see, the highlighting continue shift to the right for each new line

    Here is my code
    Code:
        Private mstrSearch As String = "aaa"
        Private mintIndex As Integer = -1
    
        Private Sub btnSearch_Click(sender As Object, e As RoutedEventArgs)
    
            mintIndex = txtMain.IndexOf(mstrSearch, mintIndex + 1)
            If mintIndex >= 0 Then
                txtMain.Focus(FocusState.Programmatic)
                txtMain.SelectionStart = mintIndex
                txtMain.SelectionLength = mstrSearch.Length
            End If
    		
        End Sub
    Last edited by Absolute_Zero; Feb 24th, 2016 at 05:23 PM.
    On Error GoTo Hell

  2. #2
    New Member
    Join Date
    Mar 2019
    Posts
    1

    Re: TextBox selection problem

    I was not able to make that technique work because the user needs to be able to work with the TextBox as a normal text box. That is to say, they need to be able to select and edit portions of the text. If I hook the click event, then the user cannot select any text to edit it.

    I was trying to use the Enter event so that it would only select all of the text the first time that the user clicked on it. If the user then works with the text (clicking to select a portion of the text to edit), I don't want to reselect the entire text.

    I found that this seemed to work for me:

    Private Sub SelectionTextBox_Enter(sender As Object, e As System.EventArgs) Handles SelectionTextBox.Enter
    Dim tb = TryCast(SelectionTextBox.TextBoxElement.TextBoxItem.HostedControl, TextBox)
    tb.BeginInvoke(New MethodInvoker(Sub() tb.SelectAll()))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width