简体   繁体   中英

Search text in richtextbox WPF C#

I'm trying to create a search text box that will allow the user to enter text, and then when the button is pressed it will searching the richtextbox to search for a match and highlight the matching text. However when reading examples of how to do this people use:

RichTextBox.Text 

but I don't have a text property, so I can't type that and can't see another way around. Why doesn't the .Text appear?

You have to use the ".Document.ContentStart" to ".Document.ContentEnd" getter since its in rich text format, with add'l formatting. There is ideally formatted text indicators and artifacts inside of your text box such as bold, paragraph properties and font types. You're going to have to set the predefined ranges using the '.Document.' property, that denotes you want just the text:

var txtReturn = TextRange(RichTextBox.Document.ContentStart,
        RichTextBox.Document.ContentEnd).Text;
Console.WriteLine(txtReturn);

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