How to follow the end of a text in a TextBox with no NoWrap?

A quick method is to measure the string (words) that needs scrolling with TextRenderer.MeasureText, divide the width measure in parts equals to the number of chars in the string and use ScrollToHorizontalOffset() to perform the scroll:

public async void TextRotation()
{
    float textPart = TextRenderer.MeasureText(words, new Font(Text.FontFamily.Source, (float)Text.FontSize)).Width / words.Length;
    for (int i = 0; i < words.Length; i++)
    {
        Text.Text = words.Substring(0, i);
        await Task.Delay(100);
        Text.ScrollToHorizontalOffset(textPart * i);
    }
}

Same, but using the FormattedText class to measure the string:

public async void TextRotation()
{
    var textFormat = new FormattedText(
        words, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight,
        new Typeface(Text.FontFamily, Text.FontStyle, Text.FontWeight, Text.FontStretch),
        Text.FontSize, null, null, 1);

    float textPart = (float)textFormat.Width / words.Length;
    for (int i = 0; i < words.Length; i++)
    {
        Text.Text = words.Substring(0, i);
        await Task.Delay(200);
        Text.ScrollToHorizontalOffset(textPart * i);
    }
}

WPF scrolling text

Leave a Comment