A Multi-Line Textbox always returns to the top when information is added to the end of the text

Lance Summers 1 Reputation point

Code Snippet

Private Sub AddResult(TextFileName As String)
Dim NewLine As String

    If (Trim(tResults.Text).Count > 0) Then
        NewLine = vbCrLf + TextFileName
        tResults.Text = tResults.Text.Insert(tResults.Text.Count - 1, NewLine)
        tResults.SelectionStart = tResults.Text.Count
        tResults.Text = TextFileName
    End If
End Sub

where tResult is a Multi-Line Text box.
when a new line is to be added to the text box, the display in the text box always returns to the top. Even though the SelectionStart was suggested as a solution.

This function is called from a routine that searches for key words in a bunch of text files so there are times I would like to scroll down to see some of the results but it keeps snapping back to the top.

My question is it even possible to have the textbox behave the way I want it to? Display the scrolled content and have it not jump up to the top when information is added to it?

Thanks, Lance

An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,506 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 110.1K Reputation points

    Make sure that the problem is not caused by some other parts of your code, and try this procedure:

    Private Sub AddResult(TextFileName As String)
        tResults.AppendText(TextFileName + Environment.NewLine)
        tResults.Select(tResults.TextLength, 0)
    End Sub
    1 person found this answer helpful.

  2. Lance Summers 1 Reputation point

    That is an improvement because it takes the Textbox display to the bottom rather than the top.
    The problem I have is when I scroll to the middle of the textbox content, the minute that a new line is appended to the textbox, the view snaps back to the bottom now and often times the snap occurs before I have a chance to see what I am looking for in the middle.

    I am going to experiment with the select to see if I can get a stable view of the middle of the Textbox content.

    Thanks for your help and giving me a new idea to work with.