Valentin Kuzub
Valentin Kuzub

Reputation: 12093

Textbox scrolling is too slow with a reasonably big text in it - anything to be done about it?

When I load around 100KB text in TextBox it begins to work really laggy. Like scrolling it, etc. It just feels bad.

Is there any reasonably easy workaround around it?

I'd like to be able to load something up to a megabyte of text without huge lag in scrolling.

On top of scrolling there are problems with selecting text or editing it, it becomes very slow and uncomfortable too.

Upvotes: 1

Views: 255

Answers (4)

This is an old thread, but the problem is still out there. I had this situation of slow scrolling TextBlock (even though I am using .net 8!). I switched to RichTextBlock with IsReadOnly="True", and the problem disappeared.

To simplify my work on a large application—which was causing multiple errors in Visual Studio—I created the following two functions:

public static void SetRichTextBoxText(RichTextBox richTextBox, string newText, bool append = false)
{
    if (append)
    {
        // Append the new text to the existing content
        System.Windows.Documents.Paragraph paragraph = new System.Windows.Documents.Paragraph(new Run(newText));
        paragraph.Margin = new Thickness(0); // Set margin to 0
        paragraph.Padding = new Thickness(0);
        richTextBox.Document.Blocks.Add(paragraph);
    }
    else
    {
        // Replace the existing content with the new text
        richTextBox.Document.Blocks.Clear(); // Clear existing content
        System.Windows.Documents.Paragraph paragraph = new System.Windows.Documents.Paragraph(new Run(newText));
        paragraph.Margin = new Thickness(0); // Set margin to 0
        paragraph.Padding = new Thickness(0);
        richTextBox.Document.Blocks.Add(paragraph);
    }
}

public static string GetRichTextBoxText(RichTextBox richTextBox)
{
    return new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
}

Upvotes: 0

Martin
Martin

Reputation: 6172

Maybe you can try a quick and cheap solution with an ItemsControl, assuming - as others did before me - that your text is separated into smaller blocks.

<ItemsControl
    ItemsSource="{Binding Path=Paragraphs}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Mode=TwoWay}" BorderThickness="0"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanelTemplate>
        <VirtualizingStackPanel/>
    </ItemsControl.ItemsPanelTemplate>
</ItemsControl>

You may need to strip down the TextBox template (MouseOver state, etc.)

Upvotes: 1

Mashton
Mashton

Reputation: 6415

The TextBox doesn't have virtualization, and so isn't really designed to support large amounts of text. You will need to use a different kind of control, and possibly think about whether your expanse of text really needs to be just a single string: could it be broken down into smaller objects that make sense?

RichTextBox, ListBox or DataGrid are all good candidates for the kind of control you should be using - depending on what you want to do and how much you can break up the text into parts.

Upvotes: 2

bit
bit

Reputation: 4487

You need to use a Control with virtualization, perhaps a DataGrid since you need the data to be editable.

It would also be better to split your data into smaller chunks.

Upvotes: 0

Related Questions