Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitlePosition a specific line at the top of a RichTextBox without flicker
KeywordsRichTextBox, scroll, line
CategoriesControls
 
Use the LockWindowUpdate API function to turn off refreshes of the RichText Box.

Set the control's SelStart property to the end of the text to establish the scrolling direction. Then set SelStart to point to line of text that should be at the top of the window.

Use LockWindowUpdate(0) to unlock and repaint the RichText Box.

Thanks to Nick Mati.

 
Private Sub GoToLine(ByVal line_number As Integer)
Dim txt As String
Dim i As Integer
Dim pos As Integer

    ' Find the line's position.
    txt = RichTextBox1.Text
    pos = 1
    For i = 2 To line_number
        pos = InStr(pos, txt, vbCrLf)
        If pos = 0 Then
            pos = 1
            Exit For
        End If
        pos = pos + 2
    Next i

    ' Go to this position.
    LockWindowUpdate RichTextBox1.hWnd
    RichTextBox1.SelStart = Len(txt)
    RichTextBox1.SelStart = pos - 1
    RichTextBox1.SelLength = 0
    LockWindowUpdate 0

    On Error Resume Next
    RichTextBox1.SetFocus
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated