BlackWaspTM

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.

TextBoxBase

In previous articles we've started to look at the methods, properties and events provided by the TextBoxBase class, which is a base class for the TextBox and RichTextBox controls. We've already seen members relating to text editing and styling of text boxes. In this article we'll see functionality related to scrolling the contents of text boxes programmatically.

To begin, we need an example project. Create a new WPF application project in Visual Studio named, "TextBoxBasePositionDemo". When the project is ready, replace the XAML of the main window with the following code:

<Window x:Class="TextBoxBasePositionDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TextBoxBase Demo"
        Width="250"
        Height="250">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <RichTextBox Name="Rtb"
                     Margin="2"
                     VerticalScrollBarVisibility="Auto"
                     HorizontalScrollBarVisibility="Auto">
            <RichTextBox.Document>
                <FlowDocument PageWidth="400">
                    <Paragraph>
                    An old queen, whose husband had been dead some years, had a beautiful
                    daughter. When she grew up, she was betrothed to a prince who lived a
                    great way off; and as the time drew near for her to be married, she got
                    ready to set off on her journey to his country. Then the queen, her
                    mother, packed up a great many costly things--jewels, and gold, and
                    silver, trinkets, fine dresses, and in short, everything that became a
                    royal bride; for she loved her child very dearly; and she gave her a
                    waiting-maid to ride with her, and give her into the bridegroom's hands;
                    and each had a horse for the journey. Now the princess' horse was called
                    Falada, and could speak.
                    </Paragraph>
                    <Paragraph>
                    When the time came for them to set out, the old queen went into her
                    bed-chamber, and took a little knife, and cut off a lock of her hair,
                    and gave it to her daughter, saying, "Take care of it, dear child; for
                    it is a charm that may be of use to you on the road." Then they took a
                    sorrowful leave of each other, and the princess put the lock of her
                    mother's hair into her bosom, got upon her horse, and set off on her
                    journey to her bridegroom's kingdom.
                    </Paragraph>
                </FlowDocument>
            </RichTextBox.Document>
        </RichTextBox>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition Width="2"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition Width="2"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center">
                Ln
            </TextBlock>
            <Button Grid.Row="1" Click="Button_Click">&lt;</Button>
            <Button Grid.Column="2" Grid.Row="1" Click="Button_Click">&gt;</Button>
            <Button Grid.Column="1" Click="Button_Click">^</Button>
            <Button Grid.Column="1" Grid.Row="2" Click="Button_Click">v</Button>
            <Button Grid.Column="4" Grid.Row="1" Click="Button_Click">&lt;&lt;</Button>
            <Button Grid.Column="6" Grid.Row="1" Click="Button_Click">&gt;&gt;</Button>
            <TextBlock Grid.Column="5" Grid.Row="1" HorizontalAlignment="Center">
                Pg
            </TextBlock>
            <Button Grid.Column="5" Click="Button_Click">^^</Button>
            <Button Grid.Column="5" Grid.Row="2" Click="Button_Click">vv</Button>
            <Button Grid.Column="8" Click="Button_Click">Home</Button>
            <Button Grid.Column="8" Grid.Row="1" Click="Button_Click">End</Button>
            <Button Grid.Column="8" Grid.Row="2" Click="Button_Click">X</Button>
        </Grid>
    </Grid>
</Window>

To complete the initial configuration, switch to the code behind the window and add the following method. We'll be adding to this code to demonstrate the scrolling features.

private void Button_Click(object sender, RoutedEventArgs e)
{
}

The resultant window appears as shown below. We'll configure each of the Buttons at the bottom of the window to scroll the contents of the RichTextBox in a different manner.

TextBoxBase scrolling demo window

22 April 2014