Results 1 to 15 of 15

Thread: RichTextBox Most Recent Text Not Updating

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    RichTextBox Most Recent Text Not Updating

    I am trying to make a chat program but when I type or add text rather.. to the richtextbox and the scroll bars appear it does not go down to where the current text is still being added. How can I make it so it is viewed at the current text just added most recently?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: RichTextBox Most Recent Text Not Updating

    For future reference, please ALWAYS provide the relevant code. If code doesn't do as intended then it's generally because there's something wrong with the code, but we can't tell what's wrong if we don't see the code.

    In this case, I can make a fairly good guess. I'll wager that you're setting the Text property of the control. In that case, you are not adding anything. You're completely replacing the existing text with new text. The caret will be placed at the beginning of the text when you do that. If you want to append new text to the existing text then you call the aptly-named AppendText method. In that case, the caret will be placed at the end of the text, meaning that the control will be scrolled so that the end of the text is visible.

    If you had no choice but to set the Text property but you still wanted to scroll to the end then you can set the SelectionStart property to the TextLength property value and then call ScrollToCaret.

    Note that all these properties and members are listed and described in the documentation for the RichTextBox class so you really should have read about them for yourself. If you want to do something with a type, reading about that type should be the first thing you do.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    ok well yes I am just adding text to the richtextbox, not sure if I am doing it correctly but it is working to add text but like I said it is not updating and by that I mean scrolling to the most recently entered text but this is what I got thus far and I am going to assume there is a better way than this but it's what I came up with:

    Code:
    RichTextBox1.Text = RichTextBox1.Text & (TextBox1.Text) & vbNewLine
    ok thanks I will try to get information on SelectionStart, TextLength and ScrollToCaret in the meanwhile ..

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: RichTextBox Most Recent Text Not Updating

    I specifically stated that you were probably setting the Text property (which you are) and that you should call the AppendText method instead and you appear to have completely ignored that. I said that setting SelectionLength and calling ScrollToCaret should be used only if you have no choice but to set the Text.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    I did not completely ignore anything that is what I had at the moment, you said previously: "For future reference, please ALWAYS provide the relevant code." and that is what I did was show you the code I was working with.. Try to relax, we are here to learn and be friendly, not stress out, please I have enough in my life already.. AppendText method, ok I need to find this on msdn and see an example. I am a very visual learner so if I can't see how to use something visually I get usually lost.. ok well this was an example from msdn they had as an example minus the textbox1.clear, it works fine but just so I know what you are trying to tell me about the appendtext method, why would appendtext be better to use in this case?

    Code:
    RichTextBox1.SelectedText = TextBox1.Text +
            Microsoft.VisualBasic.vbCrLf
            RichTextBox1.ScrollToCaret()
            TextBox1.Clear()
            TextBox1.Focus()
    Last edited by DreamWarrior77; Mar 6th, 2018 at 09:07 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: RichTextBox Most Recent Text Not Updating

    Quote Originally Posted by DreamWarrior77 View Post
    Try to relax
    What exactly makes you think that I'm not relaxed? Why is it that so many people react to the slightest criticism with an accusation at the person criticising them? I didn't say that you shouldn't have posted that code. What I said was that you should do what I actually advised you to do, which your own words indicated that you were not. I told you that you should call AppendText unless you absolutely had to set the Text property and you indicated that you weren't going to call AppendText. If you didn't ignore that instruction then why did you indicate that you weren't going to follow it? If you saw that advice and didn't ignore it then that suggests that you made a conscious decision to do something other than you were advised to do. That's your prerogative but if you have no good reason to do so then I have to wonder why I would bother providing advice again in future. If I say "do X" and you don't do X for no apparent reason, what should I think?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    I did not ignore your advice, it just so happened that I was searching for more information about what you had said and found an example using scrolltocaret that worked. No problem I have been busy with many things. Anyways like I asked you, why should I use appendtext instead of scrolltocaret. I am not disgreeing with you at all I just out of curiosity would like to know why it would be better. Is that just a more proper way of doing it? ok well I am trying to use this code but it says expected end of statement:

    Code:
    RichTextBox1.AppendText(TextBox1.Text) + vbCrLf
            TextBox1.Clear()
            TextBox1.Focus()
    Edited: ok I got it working with this:

    Code:
    RichTextBox1.AppendText(TextBox1.Text + vbCrLf)
            TextBox1.Clear()
            TextBox1.Focus()
    simple fix lol.. Thanks for helping me jmcilhinney, I really do appreciate it, always wanted to further my vb skills
    Last edited by DreamWarrior77; Mar 6th, 2018 at 09:33 PM.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: RichTextBox Most Recent Text Not Updating

    Try,
    Code:
    RichTextBox1.AppendText(TextBox1.Text & vbCrLf)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    ok well wait a sec, it is appending the text just fine but it is not going to the most recent text line still.. what have I left out?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    Thanks wes4dbt, nope it works with the + or & signs but it isnt scrolling to the most recent text for whatever reason.

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: RichTextBox Most Recent Text Not Updating

    My previous post was to just show you that the vbCrLf was in the wrong place. Using "+" will work most times but it's not the proper way to join strings and shouldn't be used. This "+" is for addition.

    This should solve your problem,
    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Me.RichTextBox1.AppendText(Me.TextBox1.Text & vbCrLf)
            Me.RichTextBox1.Select()
            Me.TextBox1.Select()
    
        End Sub

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: RichTextBox Most Recent Text Not Updating

    Hmmm... I just tested and it appears that the RTB won't scroll on its own unless/until it has focus. I must only have tested this sort of thing when the RTB already had focus before. If you explicitly call ScrollToCaret after AppendText then it will scroll as expected regardless of focus.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    haha I told ya, oh well.. but what is the best way to do this? Should I focus it, the RTB.. or just use scrolltocaret?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: RichTextBox Most Recent Text Not Updating

    You should only focus it if you want it focused. If what you want is for the control to scroll then you should call the method that exists specifically for the purpose of scrolling the control.
    haha I told ya, oh well
    Still no SelectionStart or TextLength though.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: RichTextBox Most Recent Text Not Updating

    I am sure to use those somehow soon I am sure

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