1

I wan´t to check a RichTextbox for content or better if it is empty. I debugged through the code and found that in the string i get from the box there is "\r\n". How can I check for this, because simply String.Equals("\r\n"); don´t work. For getting the text out of the RichTextbox I have this method:

 private string ConvertRichTextBoxContentsToString(RichTextBox rtb)
  {
        TextRange textRange = new TextRange(rtb.Document.ContentStart,
        rtb.Document.ContentEnd);
        return textRange.Text;
  }

Has anyone an idea for this?

kritikaTalwar
  • 1,730
  • 1
  • 17
  • 25
Only3lue
  • 245
  • 1
  • 3
  • 20
  • What if you try to compare to Environment.NewLine: https://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.110).aspx? – madoxdev Jan 19 '17 at 08:01
  • 1
    is `" "` also empty string for you? – Anton Semenov Jan 19 '17 at 08:03
  • 1
    You could check for `String.IsNullOrWhiteSpace` if you don't care about other types of white-space as well. – Manfred Radlwimmer Jan 19 '17 at 11:01
  • 1
    @Only3lue `textRange.Text.Trim().Length==0` will verify that there is no text since `Trim()` removes all leading and trailing occurrences and `\r\n` being a CRLF new line character will be removed, as well. – nam Jul 04 '19 at 17:22

3 Answers3

0

\r\n are the CRLF new line characters

have you tried the .IsEmpty property? https://msdn.microsoft.com/en-us/library/system.windows.documents.textrange.isempty(v=vs.110).aspx

Ben
  • 514
  • 2
  • 10
  • I tried your solution but it doesn´t work, because I can see that the .Text or the String itself is "/r/n". But I don´t understand why then it doesn´t work just with Equals. – Only3lue Jan 19 '17 at 09:09
0

Try this:

 if(String.IsNullOrWhiteSpace(text))
 {
    // Text found
 }
 else
 {
     // No text
 }

You can also get rid of whitspaces and new lines with text.Trim()

SirBirne
  • 297
  • 2
  • 11
  • I tried your solutions but non of them works, because I can see that the .Text or the String itself is "/r/n". But I don´t understand why then it doesn´t work just with Equals. – Only3lue Jan 19 '17 at 09:08
0

string.IsNullOrWhiteSpace should actually work. Try this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        string s = ConvertRichTextBoxContentsToString(rtb);
        if (string.IsNullOrWhiteSpace(s))
        {
            MessageBox.Show("empty!");
        }
    }
}

<RichTextBox x:Name="rtb" />

This works as well:

string s = ConvertRichTextBoxContentsToString(rtb);
if(s == "\r\n")
{
    MessageBox.Show("empty!");
}

And Equals as well:

string s = ConvertRichTextBoxContentsToString(rtb);
if (s.Equals("\r\n"))
{
    MessageBox.Show("empty!");
}

If you don't see the MessageBox the RichTextBox is not truly empty. You may want to read this:

Detect if a RichTextBox is empty

If content has been entered and removed the difference between the start and end pointers is 4. You may try to use the following method from above to determine whether the RichTextBox is empty:

public bool IsRichTextBoxEmpty(RichTextBox rtb)
{
    if (rtb.Document.Blocks.Count == 0) return true;
    TextPointer startPointer = rtb.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
    TextPointer endPointer = rtb.Document.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    return startPointer.CompareTo(endPointer) == 0;
}
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88