Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a control that inherits form RichTextBox. I found A couple of properties that are supposed to get/set the scroll position (and they work to some degree). Here is the one to get/set the vertical scroll position:
VB
    <dllimport("user32.dll",> _
Public Shared Function GetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer
End Function

<dllimport("user32.dll")> _
Private Shared Function SetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal nPos As Integer, ByVal bRedraw As Boolean) As Integer
End Function

Private Const SB_VERT As Integer = &H1
Public Property VScrollPos() As Integer
    Get
        Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT)
    End Get
    Set(ByVal value As Integer)
        SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT, value, True)
    End Set
End Property

The problem is that they don't refresh the content of the RichTextBox. The scroll bars refresh to their correct positions, but the text and everything else don't. I have tried Googling. I have also tried several calls including .Refresh(), Application.DoEvents(), and .Invalidate(), but none of them have worked as of yet. Is there anything else I can try in order to force the whole thing to redraw?

Thanks!
Ethan
Posted
Updated 1-Dec-11 19:48pm
v2
Comments
[no name] 2-Dec-11 1:48am    
EDIT: updated "pre" tag
Matchlighter 2-Dec-11 2:26am    
Thanks for doing that. I couldn't figure out how when I first posted. It usually does it by itself when I paste it from VS.

1 solution

The SetScrollPos just does that. Wont update the window.
You can use the SendMessage() function to get/set scroll position. Here's the interop

VB
Private Declare Auto Function RtfScroll _
                Lib "user32.dll" Alias "SendMessage" ( _
                ByVal hWnd As IntPtr, _
                ByVal Msg As Integer, _
                ByVal wParam As IntPtr, _
                ByRef lParam As System.Drawing.Point) As Integer

Private Const WM_USER = &H400
Private Const EM_GETSCROLLPOS = WM_USER + 221
Private Const EM_SETSCROLLPOS = WM_USER + 222


Eg to set scroll position to x=0, y =100
RtfScroll(RichTextCtl.Handle, EM_SETSCROLLPOS, 0, New System.Drawing.Point(0, 100))


To get scroll position
Dim pt As New System.Drawing.Point()
RtfScroll(rt.Handle, EM_GETSCROLLPOS, 0, pt)


==============================

There's another simpler way to achieve this without using interop.

To do it,
1. Place the cursor at the appropriate position by setting the SelectionStart property's value and setting the SelectionLength to zero.
2. call the ScrollToCaret() function to scroll to the caret
 
Share this answer
 
v2
Comments
Matchlighter 2-Dec-11 14:55pm    
I have read that the SendMessage function has a 16-bit limit. Is that anything to worry about?
strogg 3-Dec-11 8:49am    
There's no such thing. You need not worry about such misconceptions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900