Results 1 to 4 of 4

Thread: [RESOLVED] SCROLLINFO - RTB not updating

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Resolved [RESOLVED] SCROLLINFO - RTB not updating

    Hello everyone.

    I had a previous thread about Undo/Redo, but couldn't find it. Anyway, to the point:

    I use the following code to save and load the states of a RichTextBox control:
    Code:
       Public Structure FieldState
            Sub New(ByVal control As RichTextBox)
                Me.SelectionStart = control.SelectionStart
                Me.SelectionLength = control.SelectionLength
                Me.Data = control.Rtf
                Dim s As New SCROLLINFO(SCROLLINFO.Mask.Position)
                SCROLLINFO.GetScrollInfo(control.Handle, SCROLLINFO.Bar.Horizontal, s)
                Me.ScrollPosX = s.nPos
                SCROLLINFO.GetScrollInfo(control.Handle, SCROLLINFO.Bar.Vertical, s)
                Me.ScrollPosY = s.nPos
            End Sub
    
            Public Sub Apply(ByVal control As RichTextBox)
                control.Rtf = Me.Data
                control.Select(Me.SelectionStart, Me.SelectionLength)
                Dim s As New SCROLLINFO(SCROLLINFO.Mask.Position)
                s.nPos = Me.ScrollPosX
                SCROLLINFO.SetScrollInfo(control.Handle, SCROLLINFO.Bar.Horizontal, s, True)
                s.nPos = Me.ScrollPosY
                SCROLLINFO.SetScrollInfo(control.Handle, SCROLLINFO.Bar.Vertical, s, True)
                'control.Invalidate()
            End Sub
    
            Public SelectionStart As Integer
            Public SelectionLength As Integer
            Public Data As String
            Public ScrollPosX As Integer
            Public ScrollPosY As Integer
    
            Private Structure SCROLLINFO
                Public Declare Function GetScrollInfo Lib "user32" (ByVal hWnd As IntPtr, ByVal fnBar As Bar, ByRef lpScrollInfo As SCROLLINFO) As Boolean
                Public Declare Function SetScrollInfo Lib "user32" (ByVal hWnd As IntPtr, ByVal fnBar As Bar, ByRef lpScrollInfo As SCROLLINFO, ByVal redraw As Boolean) As Integer
                Sub New(ByVal mask As Mask)
                    Me.cbSize = Runtime.InteropServices.Marshal.SizeOf(Me)
                    Me.fMask = mask
                End Sub
                Private cbSize As UInteger
                Private fMask As Mask
                Public nMin As Integer
                Public nMax As Integer
                Public nPage As UInteger
                Public nPos As Integer
                Public nTrackPos As Integer
                Public Enum Bar As Integer
                    Horizontal = 0
                    Vertical = 1
                    Client = 2
                    Both = 3
                End Enum
                Public Enum Mask As UInteger
                    Range = &H1
                    Page = &H2
                    Position = &H4
                    DisableNoScroll = &H8
                    TrackPosition = &H10
                    ALL = Range Or Page Or Position Or TrackPosition
                End Enum
            End Structure
        End Structure
    Usage:
    Code:
    Dim s As New FieldState(RichTextBox1)
    s.Apply(RichTextBox1)
    This works almost 100% right, but there is one problem: the textbox does not update the scroll states. The scrollbars visibly change, but contents don't. Also, when you click the up/down buttons on the Scrollbar, it jumps to a different position and moves from there. So, how can I make that scroll information go through to the textbox control?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: SCROLLINFO - RTB not updating

    try this. it's an overload of sendmessage:

    vb Code:
    1. Private Declare Auto Function SendScrollPosMessage _
    2.                 Lib "user32.dll" Alias "SendMessage" ( _
    3.                 ByVal hWnd As IntPtr, _
    4.                 ByVal Msg As Integer, _
    5.                 ByVal wParam As IntPtr, _
    6.                 ByRef lParam As Point) As Integer
    7.  
    8. Private Const WM_USER As Integer = &H400
    9. Private Const EM_SETSCROLLPOS As Integer = WM_USER + 222

    vb Code:
    1. SendScrollPosMessage(ctrl.Handle, EM_SETSCROLLPOS, New IntPtr(0), scrollValues)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: SCROLLINFO - RTB not updating

    here's the thread you were referring to:

    http://www.vbforums.com/showthread.p...95#post3989695

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: SCROLLINFO - RTB not updating

    Thanks paul, I cleared my user thread history recently so couldn't find it back.
    I'll test the sendmessage command.

    EDIT

    Yep that works nicely, I got to look in the RichEdit.h file some more.

    Code:
        Public Structure FieldState
            Sub New(ByVal control As RichTextBox)
                Me.SelectionStart = control.SelectionStart
                Me.SelectionLength = control.SelectionLength
                Me.Data = control.Rtf
                SendMessageO(control.Handle, EM_GETSCROLLPOS, 0, Me.ScrollPosition)
            End Sub
    
            Public Sub Apply(ByVal control As RichTextBox)
                control.Rtf = Me.Data
                control.Select(Me.SelectionStart, Me.SelectionLength)
                SendMessageO(control.Handle, EM_SETSCROLLPOS, 0, Me.ScrollPosition)
            End Sub
    
            Public SelectionStart As Integer
            Public SelectionLength As Integer
            Public Data As String
            Public ScrollPosition As Point
        End Structure

Posting Permissions

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



Click Here to Expand Forum to Full Width