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:
private void ScrollToBottomOfMessages() { rtbMessages.SelectionStart = rtbMessages.Text.Length; rtbMessages.ScrollToCaret(); }
Step 2: After you add more to rtbMessages.Text, call the ScrollToBottomOfMessages() method.
That’s it.
Source code for this lesson
Next lesson: Lesson 19.2 – Use a calculated value for a property
Previous lesson: Lesson 18.1 – Future enhancements for the game
All lessons: Learn C# by Building a Simple RPG Index
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
Cool. That’s one nice thing about programming – there’s always another way to do something.
But not being a button, which follows the code inserted inside when clicked, when is this piece of code executed? Whenever we do a rtmMessages.text?
Step 2 says you’ll have to add a call to the ScrollToBottomOfMessages() function after every place where you add more text to rtbMessages.Text. I didn’t put specific location in this lesson because several of the people who asked about this made changes to their version of the program.
Search your solution (Ctrl-Shift-F) for “rtbMessages.Text” and make sure every function where you add to it, you also call ScrollToBottomOfMessages().
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!
Thanks for catching that and letting me know. You have a good day too!
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
Yes! You found an excellent opportunity for refactoring. When you do something in multiple places, it’s almost always better to call a common function.
been having some issues figuring out how to debug my code in super adventure designer and dont know if my other comment reached you so i wanted to see if you could take a look at my code.
https://prod.liveshare.vsengsaas.visualstudio.com/join?3B986E3238FD14767BC6F5D4BCC9FB258E79
thanks!
Hi,
I replied to your previous comment. I am working during the day, so I cannot do a live share.
Can you upload your solution (including the directories under it, and all the files in those directories) to GitHub, Dropbox, or some other file-sharing location so I can look at it?