Results 1 to 19 of 19

Thread: how to move text in rich text box upward

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    how to move text in rich text box upward

    Hi, I have a completed, and very well working app that I use regularly, but I would like to improve it slightly. here is the issue:
    The app records and processes astronomical observations. When it is made, a copy of each observation record appears in a Rich Text Box. Each obs. appears on a new line in the RTB. So far, so good.

    If I make quite a few observations (the norm, even in the UK!) eventually the RTB acquires a vertical scroll bar. That's fine too - but what I would REALLY like is for the text to move upwards when the RTB is full vertically, so rather than the most recent observations being 'below' the bottom of the RTB and thus invisible (unless I use the scroll bar) I would like to see them in the RTB. That does mean that the earlier obs will now themselves be invisible, 'above' the top of the RTB but that is not a problem, as I've already seen them!
    I am using VB6 on an XP laptop (yes I know it's ancient, but the laptop is only used for this one purpose!)

    Thanks in advance.

  2. #2
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    682

    Re: how to move text in rich text box upward

    So you want the RichTextBox to automatically scrolldown to show the last entries?

    How about something like this, whenever a new line is added?

    Code:
    With RichTextBox1 
       .SelStart = Len( .Text )
    End With

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,841

    Re: how to move text in rich text box upward

    Question: Do you need an RTB, or can you use another control, like a listbox or a flexgrid, for example? With those suggestions, you can easily put the last item added on the TOP ROW.

    What Phill says will work (the RTB will be 'scrolled' to that entry), but it won't be on the top row, which is what I THINK you want.
    Sam I am (as well as Confused at times).

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,234

    Re: how to move text in rich text box upward

    Quote Originally Posted by SamOscarBrown View Post
    Question: Do you need an RTB, or can you use another control, like a listbox or a flexgrid, for example? With those suggestions, you can easily put the last item added on the TOP ROW.

    What Phill says will work (the RTB will be 'scrolled' to that entry), but it won't be on the top row, which is what I THINK you want.
    With the other controls you would get the same result, because if you were able to set the last row to the top, you would always see only one row (the last added).

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,841

    Re: how to move text in rich text box upward

    Not necessarily....depends upon what was added and when...can sort those controls by some 'added' information (date/time, number assigned, etc).

    BUT, I see your point...probably what Phill posted is OP's best option as he already has the RTB in this (old) program.

    Sam
    Sam I am (as well as Confused at times).

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,234

    Re: how to move text in rich text box upward

    I believe the OP adds new lines at the end (the bottom of the list).

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,346

    Re: how to move text in rich text box upward

    Maybe something like this using DrawText() calls?

    Name:  sshot.png
Views: 158
Size:  3.7 KB

    Code:
    Private Sub Timer1_Timer()
        Dim NewText As String
        Dim RECT1 As RECT
    
        'Generate some sample text:
        NewText = WeekdayName(Int(Rnd() * 7) + 1, True, vbSunday) & vbTab _
                & Format$(Format$(Rnd() * 10000 - 5000, _
                                  "+#,##0.000;-#,##0.000;""ZERO"""), _
                          "@@@@@@@@@@") & vbTab _
                & ChrW$(Int(Rnd() * 26) + AscW("A")) & vbTab _
                & ChrW$(Int(Rnd() * 26) + AscW("a")) & vbTab _
                & ChrW$(Int(Rnd() * 26) + AscW("A")) & vbTab _
                & MonthName(Int(Rnd * 12) + 1)
        NewText = Replace$(NewText, "&", "&&") 'We can't use DT_NOPREFIX with DT_TABSTOP!
        Lines = Lines + 1
        If Lines > 1 Then
            If Lines > MaxLines Then
                Text = Mid$(Text, InStr(Text, vbNewLine) + 2) & vbNewLine & NewText
                Lines = Lines - 1
            Else
                Text = Text & vbNewLine & NewText
            End If
        Else
            Text = NewText
        End If
        With Picture1
            .AutoRedraw = True
            .Cls
            RECT1 = RECT
            DrawText .hDC, _
                     StrPtr(Text), _
                     -1, _
                     RECT1, _
                     DT_EXPANDTABS _
                  Or DT_WORDBREAK _
                  Or DT_TABSTOP _
                  Or &H400& 'Tabs every 4 chars.
            .AutoRedraw = False
        End With
    End Sub
    Attached Files Attached Files
    Last edited by dilettante; Feb 9th, 2021 at 10:13 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,346

    Re: how to move text in rich text box upward

    Still not sure I guessed what is needed correctly, but here is a "no API" attempt implementing a simple rich markup syntax:

    Name:  sshot.png
Views: 150
Size:  5.5 KB
    Attached Files Attached Files

  9. #9
    Addicted Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    135

    Lightbulb Re: how to move text in rich text box upward

    Quote Originally Posted by starman1500 View Post
    I would like to see them in the RTB
    SendMessage(hRichEdit, EM_SCROLLCARET, 0, 0)

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: how to move text in rich text box upward

    Hi Phill,
    Thanks for your helpful idea. But I have just discovered that what actually happens is that the data goes to a text file, and then the text file is loaded into the RTB - rather than each line of text being written directly to the RTB. Sorry about that - but the laptop is normally in my observatory rather than in the house! I am assuming that the various kind replies I have had won't actually work now! Again, sorry about that. I suppose I can write to both the text file AND directly to the RTB too, in which case your idea could be put into operation!

  11. #11
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    682

    Re: how to move text in rich text box upward

    Quote Originally Posted by starman1500 View Post
    ... the data goes to a text file, and then the text file is loaded into the RTB ...
    Very common.

    Have your code keep a note of the size (length) of the file.
    Periodically, poll the size of the file. If it's changed, open the file, Seek to the position you "remembered", then read the rest of the file and make a new note of the new file length.
    Add the text you've just read in onto the end of your RTB.

    Regards, Phill W.

  12. #12
    Addicted Member jj2007's Avatar
    Join Date
    Dec 2015
    Posts
    135

    Re: how to move text in rich text box upward

    Hi starman,

    The EM_SCROLLCARET message was designed to do what you want. Just test it.

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,294

    Re: how to move text in rich text box upward

    Phil's code in post #2 should still work.
    After the file is reloaded in the RTB, then set the select start position to the end of the textbox.
    Of course, jj2007 code will also work, you just have to add a bit of API declaration and look up a constant.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  14. #14

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: how to move text in rich text box upward

    Yes, I thought there was a scary bit of API-ing going on there! Thanks everyone, I shall give it a try tomorrow.

  15. #15
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,234

    Re: how to move text in rich text box upward

    You can ensure that the scroll will be always at the end without caring when the text is updated by placing the code in the Change event:

    Code:
    Private Sub RichTextBox1_Change()
        RichTextBox1.SelStart = Len(RichTextBox1.Text)
    End Sub

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,346

    Re: how to move text in rich text box upward

    Wasteful, but sure, you can drag the text out of the control and convert it to a String; measure it; then throw it away like that. Ugh.

  17. #17
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,346

    Re: how to move text in rich text box upward

    Quote Originally Posted by starman1500 View Post
    But I have just discovered that what actually happens is that the data goes to a text file, and then the text file is loaded into the RTB - rather than each line of text being written directly to the RTB.
    If I wanted to leave that nasty behavior in place, and I wanted to keep using an RTB, etc. then I'd probably just use TOM with it instead. TOM will probably reload this data faster than anything else but raw API calls, and it can easily be used to move the insertion point to the end or even just above the end (which is probably what you really want: no blank line at the bottom).

    Tons of threads here and in the CodeBank on TOM usage with a RichTextBox.

  18. #18
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,234

    Re: how to move text in rich text box upward

    Yes, there are many things that are "wasteful" in programming, but it does not worth (or even makes sense IMO) to optimize things that do not need to be optimized.
    Unless he has many thousands of lines, there will be no practical inpact.

    I believe that programming, as almost anything in life, needs balance. In this case it is a balance between simplicity and optimization.
    I think it is more important in a case like this to resolve the issue in three lines, than applying complex code in the sake to optimize 10 milliseconds.

  19. #19

    Thread Starter
    New Member
    Join Date
    Sep 2018
    Posts
    4

    Re: how to move text in rich text box upward

    It worked - as you guys promised it would! Many thanks for your collective help. I used Phill's nice and simple solution in the end, so once again thanks Phill and others.

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