0

This is a text editor. I am trying to open a new file and if the existing file is not empty, users get a warning to save the file or not. But if I use the condition if (text== ""), the value of text still has "\r\n" new line value. Even I didn't click on the text area. Why does string get the value of the new line? It doesn't have to be empty?

private void New_Click(object sender, RoutedEventArgs e)
{
    var text = new TextRange(richtxtbox.Document.ContentStart, richtxtbox.Document.ContentEnd).Text; 

    if (text == "")
    {
        MainWindow newWindow = new MainWindow();
        newWindow.Show();
        Window.GetWindow(this).Close();
    }
    else
    {
        // ...       
    }
}

I have to do this:

if (text == "" || text == "\r\n")
{
    MainWindow newWindow = new MainWindow();
    newWindow.Show();
    Window.GetWindow(this).Close();
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Kalo
  • 21
  • 6
  • 1
    Use [TextRange.IsEmpty](https://learn.microsoft.com/en-us/dotnet/api/system.windows.documents.textrange.isempty?view=windowsdesktop-6.0) to check if a range is empty or not. – Panagiotis Kanavos May 27 '22 at 12:35
  • Using `TextRange.Text` is an expensive operation that [creates and returns a new string on each call](https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/windows/Documents/TextRangeBase.cs,676) and shouldn't be called frequently – Panagiotis Kanavos May 27 '22 at 12:41
  • Did you copy the code [from this question?](https://stackoverflow.com/questions/5825575/detect-if-a-richtextbox-is-empty) Using a TextRange just to check whether the document is empty is very wasteful – Panagiotis Kanavos May 27 '22 at 12:59
  • I declare Richtextbox without FlowDocument. I have added FlowDocument to Richtextbox. it works. TextRange.IsEmpty returns true but without FlowDocument returns false. @PanagiotisKanavos – Kalo May 27 '22 at 13:28

1 Answers1

3

You wrote:

I am trying to open a new file and if the existing file is not empty...

What do you mean "the existing file is not empty"? I guess you mean the current content of the RichtextBox, that actually is container for the FlowDocument object. Right?

Result of the var text = new TextRange(richtxtbox.Document.ContentStart, richtxtbox.Document.ContentEnd).Text depend on content of the FlowDocument.

If you declare RichTextBox as below than text will contain "\r\n". It's because the FlowDocument will contain one Paragraph block. And when this paragraph is converted to text it will automatically appended with the Environment.NewLine.

<RichTextBox Name="richtxtbox">
    <FlowDocument>
        <Paragraph>          
        </Paragraph>
    </FlowDocument>
</RichTextBox>

But if you declare RichTextBox as empty FlowDocument than text will be empty - it contains 0 blocks.

<RichTextBox Name="richtxtbox">
    <FlowDocument>
    </FlowDocument>
</RichTextBox>

For more information read the following article: RichTextBox Overview

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • TextRange has an `IsEmpty` property that's far cheaper than creating a new string with the serialized contents every time – Panagiotis Kanavos May 27 '22 at 12:42
  • @PanagiotisKanavos: In the case when the document contains an empty paragraph (visually it looks like an empty document) the `TextRange.IsEmpty` will return `false` and this will confuse the user. Therefore call the `IsEmpty` will not help a much. – Jackdaw May 27 '22 at 12:51
  • The question here is whether the control is empty or not. Right now, it's not. A single empty paragraph is still a paragraph. If the TextRange is only used to get the text ... it shouldn't be used in the first place – Panagiotis Kanavos May 27 '22 at 12:54
  • Yes, I mean the current content of the RichtextBox. I think I found the problem, I used RichtextBox without FlowDocument. ` ` @Victor – Kalo May 27 '22 at 13:38
  • @Kalo: Actually, you don't need to explicitly create the `FlowDocument` object. The `RichTextBox` has parameterless constructor that creates implicit instance of a `FlowDocument` as its initial content. And the initial document will contain one `Paragraph` with an empty `Run` in it. You can see this in the sources here: [https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/RichTextBox.cs](https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Controls/RichTextBox.cs) – Jackdaw May 27 '22 at 14:53