Results 1 to 12 of 12

Thread: bring a richtextbox line to the top of the view

  1. #1

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    bring a richtextbox line to the top of the view

    i'd like to bring a line of text to the of the RTB.

    specifically, i'm using the RTB's find method, and would like the line that has the found selection moved to the top of the RTB. no text manipulation, just manipulation of which section of text is in view of the RTB.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: bring a richtextbox line to the top of the view

    You can send the EM_LINESCROLL message to the RTB to do this. I wrapped it up in a simple Sub called SetTopIndex to which you pass a reference to the RichTextBox and the zero based line number you want to scroll to. You can get the line number by calling the GetLineFromChar method of the RTB.
    VB Code:
    1. Private Declare Function SendMessage _
    2.  Lib "user32.dll" Alias "SendMessageA" ( _
    3.  ByVal hwnd As Long, _
    4.  ByVal wMsg As Long, _
    5.  ByVal wParam As Long, _
    6.  ByRef lParam As Any) As Long
    7.  
    8. Private Const EM_GETFIRSTVISIBLELINE As Long = &HCE
    9. Private Const EM_LINESCROLL As Long = &HB6
    10.  
    11. Public Sub SetTopIndex(rtb As RichTextBox, ByVal nLine As Long)
    12.     Dim nIndex As Long
    13.     nIndex = nLine - SendMessage(rtb.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
    14.     Call SendMessage(rtb.hwnd, EM_LINESCROLL, 0&, ByVal nIndex)
    15. End Sub

  3. #3
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: bring a richtextbox line to the top of the view

    You may also want to look at this project from PSC.
    It also has methods for getting the line numbers.

    Look for this sub: "SetScrollPos"

    http://www.planet-source-code.com/vb...34340&lngWId=1

  4. #4

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: bring a richtextbox line to the top of the view

    well, something is wrong here. the RTB is scrolling to the last line and putting that on top. even if i manually put in a value, such as below
    VB Code:
    1. SendMessage RichTextBox1.hwnd, EM_LINESCROLL, 0&, 10
    it still scrolls to the last line

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: bring a richtextbox line to the top of the view

    You must pass the lParam argument ByVal, and it will scroll that many lines from the current position. Did you try my Sub that calculates the number of lines you have to scroll to get to the line you want?

  6. #6

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: bring a richtextbox line to the top of the view

    wow, my fault. i really didn't think getting rid of ByVal would make a difference!

    now then, the easiest way to get the top line at the top? ie, scrolled all the way up.

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: bring a richtextbox line to the top of the view

    Quote Originally Posted by dis1411
    now then, the easiest way to get the top line at the top? ie, scrolled all the way up.
    What do you mean? Do you want to know which line there is at the top? If that is the case just send the EM_GETFIRSTVISIBLELINE message (also shown in my first example).

  8. #8

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: bring a richtextbox line to the top of the view

    well, EM_LINESCROLL scrolls down, but i also need to scroll all the way up to the beginning of the document
    Last edited by dis1411; May 26th, 2005 at 03:20 PM.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: bring a richtextbox line to the top of the view

    If you use my sub just call it with 0 as the line number.
    VB Code:
    1. Call SetTopIndex(RichTextBox1, 0)
    Or if you want to write your own code just pass a negative number in lParam when you send the EM_LINESCROLL to scroll upwards. The number of lines you need to scroll up can be retrieved by sending EM_GETFIRSTVISIBLELINE.

  10. #10

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: bring a richtextbox line to the top of the view

    jeez, i posted too soon again. you can also do rtb.selstart = 0. in my case i accidentally did something after that which cancelled the effect

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: bring a richtextbox line to the top of the view

    Quote Originally Posted by dis1411
    jeez, i posted too soon again. you can also do rtb.selstart = 0. in my case i accidentally did something after that which cancelled the effect
    That's true. But that will also change the caret position but maybe you want to do that.

  12. #12

    Thread Starter
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: bring a richtextbox line to the top of the view

    for my purposes, it's no matter. 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