简体   繁体   English

在Richtextbox WPF C#中搜索文本

[英]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. 我正在尝试创建一个搜索文本框,允许用户输入文本,然后当按下按钮时,它将搜索richtextbox来搜索匹配项并突出显示匹配的文本。 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. 但是我没有text属性,所以我无法输入该属性,也看不到其他方法。 Why doesn't the .Text appear? .Text为什么不出现?

You have to use the ".Document.ContentStart" to ".Document.ContentEnd" getter since its in rich text format, with add'l formatting. 您必须使用“ .Document.ContentStart”到“ .Document.ContentEnd”吸气剂,因为它是RTF格式,具有add'l格式。 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.' 您将必须使用“ .Document”设置预定义范围。 property, that denotes you want just the text: 属性,表示您只需要文本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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