This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Base Classes - TextBoxBase - Scroll Position

The fifty-seventh part of the Windows Presentation Foundation Fundamentals tutorial continues to look at members of the TextBoxBase class. This article examines the methods that can be used to scroll text box contents.

Scrolling Methods

There are several methods provided by TextBoxBase that permit you to scroll a TextBox or RichTextBox. Most provide a way to scroll content that matches the expectations of the user. You can scroll in large or small steps, as you might with the arrow keys or page up and page down keys, jump directly to the start or end of the box, as with the Home or End keys, or scroll to a specific position.

Line Scrolling Methods

For small movements, use one of four "Line" methods. These are LineUp, LineDown, LineLeft and LineRight. You can see these in action by adding the following code within the Button_Click method. The code reads the content from the clicked button and employs a switch statement to decide which method to execute. NB: Switching based upon button content is not advisable in production software.

var button = sender as Button;
if (button == null) return;
string cmd = button.Content.ToString();

switch (cmd)
    case "<": Rtb.LineLeft(); break;
    case ">": Rtb.LineRight(); break;
    case "^": Rtb.LineUp(); break;
    case "v": Rtb.LineDown(); break;

Run the program and try scrolling the text box using the four buttons to the left of the window.

Page Scrolling Methods

For larger movements, you can execute the "Page" methods. These are PageUp, PageDown, PageLeft and PageRight. They scroll the content of a text box by an amount similar to the visible area. To see them in action, add the following four lines within the switch statement.

case "<<": Rtb.PageLeft(); break;
case ">>": Rtb.PageRight(); break;
case "^^": Rtb.PageUp(); break;
case "vv": Rtb.PageDown(); break;

Run the program and use the buttons to compare page and line scrolling.

ScrollToHome and ScrollToEnd

Two further methods allow you to quickly scroll a text box's content to see either the start or the end of the text. These are ScrollToHome and ScrollToEnd. The former scrolls to the start of the content and the latter to the foot of the text. To add these to the sample application, include the following two cases in the switch statement.

case "Home": Rtb.ScrollToHome(); break;
case "End": Rtb.ScrollToEnd(); break;

Run the program and try clicking the Home and End buttons to see the results.

Scrolling to a Fixed Position

The final two methods allow you to scroll the content of a text control to show a specific position. The methods are ScrollToHorizontalOffset and ScrollToVerticalOffset. Each has a single parameter, which receives a decimal value determining the position of scrolling. You can retrieve the current position with the HorizontalOffset and VerticalOffset properties.

To try the methods, add the following case to the switch, to detect when the "X" button is clicked:

case "X": ScrollToFixedPosition(); break;

Now add the private ScrollToFixedPosition method shown below. When the button is clicked, this code will scroll to a position fifty pixels across and one hundred pixels down from the top corner of the RichTextBox.

private void ScrollToFixedPosition()
22 April 2014