# Visual Basic > Universal Windows Platform and Modern Windows Experience >  TextBox selection problem

## Absolute_Zero

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


```
aaa-aaa-
aaa-aaa-
aaa-aaa-
```

and search for "aaa", tapping on Find Next button produce the following highlighting sequence



```
aaa-aaa-
aaa-aaa-
aaa-aaa-
```



```
aaa-aaa-
aaa-aaa-
aaa-aaa-
```



```
aaa-aaa-
aaa-aaa-
aaa-aaa-
```



```
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


```
    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
```

----------


## amzoun95

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()))

----------


## VB.NET Developer

@amzoun95 - post relevant code with code tags in order to improve readability.

----------


## Delaney

Hello,

For me, this correction works :



```
public mstrSearch As String = "aaa"
public mintIndex As Integer = -1

    Private Sub btnSearch_Click(sender As Object, e As RoutedEventArgs)

        mintIndex = txtMain.text.IndexOf(mstrSearch, mintIndex + 1) 'here I added .text as indexof in not a method of textbox but of text
        If mintIndex >= 0 Then
            txtMain.Focus() 'I removed FocusState.Programmatic as I don't have the UI.XAML library installed but it just show block instead of coloring the letters
            txtMain.SelectionStart = mintIndex
            txtMain.SelectionLength = mstrSearch.Length
        End If
		
    End Sub
```

result after 6 clicks :


Regards

----------


## shMelbin

I have seen other questions for this and none of them have sufficient answers. With the 2018 update, I now have alot of trouble with text selection.

If I were to take a stab at specifying the case, here it is.  When I am editing text and I have a text box in scope, I am accustomed to being able to easily marquee select text for highlighting / editing (in 2017 and prior). Now, with 2018, it often creates a NEW text element when I try to marquee select text I am editing. I believe that in the past, I could start a Marquee selection with my mouse outside the bounds of the text box. But in 2018, that will now create a new text box. So I must be extremely precise with the marquee selection start point, at Illustrator is very finicky about it. In fact it requires not only that I be within the the text box - but that my cursor is actually over text IN the box at the time the marquee selection starts. This is very limiting, as it is virtually impossible to group select the exact text you want as you only have about a pixel or two beyond the last character before the cursor is considered out of scope.

Looks like alot of other people have had this problem, and yes I have tried checking Preferences-> Type and "type Objection Selection by Path Only" is not checked. And checking it makes it even harder to select text.

Thank you.

----------

