How can I compare a richtextbox to a string in WPF/c#

Viewed 54
private void richTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            string _Text = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;

            if ("time".Equals(_Text.ToLower()))
            {
                MessageBox.Show("test");
            }

        }

I am not sure if the XAML code is necessary but it is basically just a Richtextbox. I am confused why when I type "Time" the message box doesn't show up. I have tried equals, I have tried starts with, I have tried contains. This works fine on my textbox but not with a richtextbox. Any help is appreciated, thanks!

1 Answers

use this code :

private void richTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    TextRange textRange = new TextRange(richTextBox.Document.ContentStart,richTextBox.Document.ContentEnd);
    string _Text = textRange.Text.Replace(Environment.NewLine, "");
    if (_Text.ToLower().Equals("time"))
    {
       MessageBox.Show("test");
    }
}
Related