简体   繁体   中英

C# WPF RichtextBox type behavior change

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?)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM