C# WPF RichtextBox type behavior change

386 Views Asked by At

I have recently converted a TextBox (which I am using as a search box) to a RichTextBox in order to bold some contained words within the box. Upon doing so I have noticed a few "errors". I would like to know if they are intentional, and how to over-ride them.

The first, and most important problem occurs only where I call a SelectAll() on the box to pull its information on the keyDown event:

((RichTextBox)sender).SelectAll();
string search = ((RichTextBox)sender).Selection.Text;

This prevents me from writing more than a character in the RTB. I presume that it is leaving my text selected and overwriting it with every key press. I cannot find any DeSelect() method, so is there another means of attaining this string data to prevent this issue. I need the string to pass to a regular expression.

Secondly, the RTB only throws the IBeam into the box on a double click. Is there a way to recreate the behavior of a TextBox that enters on a single click (or should it be entering on a single click by default?)

1

There are 1 best solutions below

0
On BEST ANSWER

Instead of selecting all, get the content and convert the flow document to plain text.

FlowDocument doc =((RichTextBox)sender).Document;
string search = new TextRange(doc.ContentStart, doc.ContentEnd).Text;

Convert FlowDocument to simple text