Lesson 19.1 – Scroll to the bottom of a rich text box

When you play the game, you probably noticed the messages RichTextBox scrolls to the top after you add more messages to it. So, the player has to manually scroll to the bottom to see the latest message.

We want to make it easier for the player.

 

How to scroll to the bottom of a RichTextBox

Step 1: Add this new ScrollToBottomOfMessages() function to the code in the SuperAdventure.cs class:

Step 2: After you add more to rtbMessages.Text, call the ScrollToBottomOfMessages() method.

 

That’s it.

 

Source code for this lesson

Get it from GitHub: https://gist.github.com/ScottLilly/21997dc89b21c629c449

or DropBox: Lesson 19.1 – https://www.dropbox.com/sh/hk08rmdqeef6sin/AACDHvF9dalLY_NnrTmVZ5FEa?dl=0

Previous lesson: Lesson 18.1 – Future enhancements for the game

Next lesson: Lesson 19.2 – Use a calculated value for a property

All lessons: Learn C# by Building a Simple RPG Index

6 thoughts on “Lesson 19.1 – Scroll to the bottom of a rich text box

  1. Hello,
    I just double-clicked on the rtbMessages RichTextBox and it created the function:
    private void rtbMessages_TextChanged(object sender, EventArgs e)

    I added:
    rtbMessages.SelectionStart = rtbMessages.Text.Length;
    rtbMessages.ScrollToCaret();

    Now i don’t have to insert a function everytime it changes the text

  2. Hey Scott,

    great tutorial so far, helped me a lot understanding the basics of C#.

    Just want to let you know that next and previous lesson are mixed up :p

    Thanks again.

    Have a nice day!

  3. When checking out this idea, I instead made a DisplayMessage function

    private void DisplayMessage(string msg)
    {
    rtbMessages.Text += msg;
    rtbMessages.SelectionStart = rtbMessages.Text.Length;
    rtbMessages.ScrollToCaret();
    }

    Is that a good idea for potential refactoring?

    I was thinking yes due to how you could constantly change where the messages were displayed

Leave a Reply

Your email address will not be published. Required fields are marked *