Results 1 to 10 of 10

Thread: How to set the scroll position in a RTB

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    5
    Hi all,


    I am designing some sort of a Text viewer in which a user can change the FontColor. I use a RichTextBox so that filesize is not limited to 32k. When I change the Text color, I use RTB options as .SelStart, .SelLen, .SelColor, etc.

    However, when -in a large Text- you scrolled down and then change the Text color, the scrollbar is 'reset' to the top (or focused on the cursor position).

    What I want now is that, effectively, changing the Text color does not affect the Scroll position in the RTB (just as setting the .BackColor property doesn't, or the ForeColor in a ordinary TextBox doesn't). I've tried the user32 functions GetScrollPos and SetScrollPos. The latter however, sets ONLY the Scoll Bar position, but leaves the Text position unchanged, so that only half of the job is done (you get the odd situation that you see the top of the text but the scrollbar is e.g. half way down...).


    Can someone help me with this?
    Thanx!

    Gildor

  2. #2
    Guest
    Set the SelStart property.

    Code:
    RichTextBox1.SelStart = 100

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    5
    Thanx Megatron, but setting the .SelStart would only set the cursor to that position, not the ScrollBar, right?


    I have now something like this:

    With RichTextBox1
    CursorPos = .SelStart 'Remember current Cursor Pos
    ScrollPos = GetScrollPos(.hWnd, 1) 'Get scrollbar postition
    .Visible = False 'Don't show the selecting part
    .SelStart = 0
    .SelLen = Len(.Text)
    .SelColor = NewColor
    .SelLen = 0
    .Visible = True
    .SelStart = CursorPos
    Dummy = SetScrollPos(.hWnd, 1, ScrollPos, 1)
    End With

    Again, SetScrollPos sets only the position of the scrollbar, but the text is still focussed at the cursor position (.SelStart), which is not necessarily the same.


    Hope this clears things up,
    Regards,
    Gildor

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Try this (change the variable and object name as necessary):
    Code:
    Private Declare Function SendMessage _
     Lib "user32" Alias "SendMessageA" ( _
     ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     lParam As Any) As Long
    
    Private Const EM_LINESCROLL = &HB6
    Private Const EM_GETFIRSTVISIBLELINE = &HCE
    Private Const SB_LINEDOWN = 1
    Private Const SB_LINEUP = 0
    
    Private Sub Command1_Click()
        Dim lngFirstLine&
        Dim lngCurrentLine&
        Dim lngSelStart&
        
        Randomize
        With RichTextBox1
            lngSelStart = .SelStart
            lngFirstLine = SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
            .Visible = False
            .SelStart = 0
            .SelLength = Len(.Text)
            .SelColor = QBColor(Int(Rnd * 16))
            .SelStart = lngSelStart
            lngCurrentLine = SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
            If lngCurrentLine < lngFirstLine Then
                Do
                    SendMessage .hwnd, EM_LINESCROLL, SB_LINEDOWN, 0&
                Loop While SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) <> lngFirstLine
            ElseIf lngCurrentLine > lngFirstLine Then
                Do
                    SendMessage .hwnd, EM_LINESCROLL, SB_LINEUP, 0&
                Loop While SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) <> lngFirstLine
            End If
            .Visible = True
        End With
    End Sub
    Good luck!

  5. #5
    Hyperactive Member gravyboy's Avatar
    Join Date
    Jan 2000
    Location
    Where I was before . . . if you don't know then you're new!
    Posts
    334

    Scroll Position

    Is the caret in the same area as the scroll box position when you do the colour action?
    Matt G
    VS6 Ent SP5 @ Work
    VS6 Ent SP5 & VB.Net @ Home
    [email protected]



  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    5
    I finally managed to control the scrollbar, using something similar to what Joacim suggested: the SendMessage function and the EM_LINESCROLL constant.

    Thanx all for your attention,
    Gildor

  7. #7
    Guest
    Thanx Megatron, but setting the .SelStart would only set the cursor to that position, not the ScrollBar, right?
    It will move the cursor position as well as the ScrollBar. I know it might sound "to good to be true" but it works.


  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by Megatron
    It will move the cursor position as well as the ScrollBar. I know it might sound "to good to be true" but it works.
    What I think Gildor wants is to scroll beyound the cursor position. The cursor can for example be at the first line but the first visible line i the RTF-box may be the 10th line.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    5
    Joacim, you are absolutely right ;
    I couldn't have set it better! (Otherwise there wouldn't have been so much confusion )

    Anyways, I got it working now... Thanx again!
    Gildor

  10. #10
    Zoinky
    Guest

    Related Question

    I'm trying to do the same type of thing, except I want to do this:

    1) Check to see if scrollbar is at the bottom of the RichTextBox
    2) Add a line of text (and add colouring) to the end.
    3) If the scrollbar WAS at the bottom, scroll down. (I can do this), BUT, if it was somewhere else, return to that spot. (I can do this as well, with the information you just gave.)

    What I can't seem to figure out how to do, however, is to check to see if the scrollbar is at the bottom. I initially thought about checking to see if .SelStart = Len(.Text), but the cursor may not be at the very end of the RichTextBox. (User may have clicked somewhere else in the RichTextBox.) If it's not too much to ask, perhaps someone could expand on this method, to check where the scrollbar is at the beginning? Perhaps some sort of method to determine the LAST visible line in the RichTextBox would be the key? And then comparing it to the TOTAL number of lines?

    Thanks!

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