erotavlas
erotavlas

Reputation: 4485

Why does RichTextBox always contain extra characters before the document start?

I start with a blank RichTextBox

    <RichTextBox x:Name="textBox" HorizontalAlignment="Left" Height="234" Margin="10,75,0,0" VerticalAlignment="Top" Width="214" PreviewMouseLeftButtonUp="textBox_PreviewMouseLeftButtonUp">
    </RichTextBox>

And then I load a file, read all text and then add this to the RichTextBox like this

using (StreamReader sr = new StreamReader(@"D:\Text.txt"))
{
    string fullText = sr.ReadToEnd();

    textBox.Document.Blocks.Add(new Paragraph(new Run(fullText)));
}

When I inspect the fullText that I insert, it looks like this...immediately starts with the character E

enter image description here

But then after its rendered into the control, I an selecting a part of the beginning of text, but there appear to be a carriage return line feed before the very first character.

enter image description here

enter image description here

So my question is does it always insert these characters?

Is there a way to remove this behavior, so it doesn't insert anything before the very first character of the document?

Upvotes: 2

Views: 419

Answers (1)

Blake Thingstad
Blake Thingstad

Reputation: 1659

Your RichTextBox has a Block already in it. You can remove it with textbox.Document.Blocks.Clear();. Your first new paragraph will now not have space above it. Paragraphs add space above themselves if they come after another element.

Upvotes: 2

Related Questions