搜索 RichTextBox 并突出显示该特定单词的所有实例的方法
我真的不知道应该从哪里开始。
我有一个带有 RichTextBox
的 WPF 应用程序,其中有大量使用 FlowDocument
的文本,该文本会根据用户的选择而变化。
我需要一种方法,用户可以在 TextBox
中输入单词,并且如果找到该单词的每个实例,则将在相邻的 RichTextBox
中突出显示。 http://kentb.blogspot.com/2009 /06/search-and-highlight-text-in-任意.html 这个想法很完美,但我对如何使用 RichTextBox
将其应用到我的应用程序一无所知。
先感谢您!
I'm really clueless with where I should begin to start with this.
I have a WPF application which has a RichTextBox
, inside this there is a load of text using a FlowDocument
which changes depending upon the user's selection.
I need a method from which a user can type a word into a TextBox
and every instance of this word if it is found will then be highlighted with in the adjacent RichTextBox
. http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.html This idea would be perfect but I am clueless as how to apply it to my application with a RichTextBox
.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试过使用正则表达式吗?
类似于:
只要您的盒子中不需要同时使用多种颜色,此方法就有效。我确信,通过一些额外的逻辑,您也可以将其纳入其中。
编辑:在 WPF 中不起作用。持续关注 WinForms。
Have you tried using RegularExpressions?
Something like:
As long as you don't need multiple colors in your box at the same time, this method works. With some extra logic you could incorporate that, too, I'm sure.
edit: doesn't work in WPF. Keeping post up for WinForms.
我使用 FlowDocument 来完成此操作。此示例列出了具有该颜色背景的颜色。我使用 FlowDocumentReader 来显示 FlowDocument,但我认为 RichTextBox 也会显示 FlowDocument。它可能看起来有点复杂,但标记实际文本比必须突出显示某个位置(就像我必须使用
Windows.Form RichTextBox
一样)的问题要少得多。这是我用来决定哪种颜色突出显示看起来最好的代码。I do it with a
FlowDocument
. This sample lists the colors with a background of that color. I useFlowDocumentReader
to display theFlowDocument
but I think aRichTextBox
will also display aFlowDocument
. It might seem a little complex but marking up the actual text is way less problematic than having to highlight a position like I had to back withWindows.Form RichTextBox
. This is code I used to decide what color highlight looked the best.