This is a migrated thread and some comments may be shown as answers.

"Find whole words only" option in Find/Replace dialog?

4 Answers 119 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
JMSteele
Top achievements
Rank 1
JMSteele asked on 27 Jun 2013, 05:30 PM
Something caught my eye while toying with online RichTextBox demo--namely that the Find/Replace dialog does not include a "find whole words only" option. Tried Replace All   "he" with "she" and, obviously, it replaced all occurrences of the letters "he" in the document.

1) What would be the simplest way to implement this  functionality.
2) If this is not currently available as an option that can be enabled within the default Find/Replace, take this as a suggestion to please add this as a feature in the future. It is, after all, a very basic feature that I suspect most users would expect.

Much thanks for your time and consideration.

4 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 01 Jul 2013, 07:40 AM
Hi Jim,

Thank you for the feature suggestion. We will consider adding such an option to the dialog internally.

Meanwhile, you can create and use your own FindReplaceDialog. RadRichTextBox uses MEF to load the dialogs, context menus and toolbars of its UI. In this regard, in order to replace a dialog, the only thing that needs to be done is to implement an interface and mark the custom dialog with the respective attribute. For starters, you can refer to this demo on Git Hub which shows how the default FindReplaceDialog can be used as a custom one.

When it comes to the search, RadDocument uses DocumentTextSearch, which allows searching in the document against regular expressions. You can read more in the documentation on Search. In a nutshell, in order to match the whole word only, you should add the regex-specific characters that match the boundaries of a word ("\b") before and after the string specified by the user:

string findText = "\\b" + textToSearch + "\\b";
DocumentTextSearch search = new DocumentTextSearch(this.radRichTextBox.Document);
foreach (var textRange in search.FindAll(findText))
{
    textRange.SetSelection(this.radRichTextBox.Document);
}
I hope this helps!

Regards,
Iva Toteva
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
JMSteele
Top achievements
Rank 1
answered on 01 Jul 2013, 03:37 PM
Thanks for such a detailed response, Iva. Very helpful.
0
Jaypee
Top achievements
Rank 1
answered on 23 Jul 2013, 06:28 AM
how if i dont want to use regular epression. can i disable "use regular epression"?
0
Petya
Telerik team
answered on 25 Jul 2013, 04:16 PM
Hello Jaypee,

By default, the FindReplaceDialog escapes the Text to find, so I am presuming you are referring to the DocumentTextSearch class, is that correct?

The public methods of the class - Find() and FindAll(), expect a regex, however you can escape the regex before passing it to the method.

I hope this helps!

Regards,
Petya
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
RichTextBox
Asked by
JMSteele
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
JMSteele
Top achievements
Rank 1
Jaypee
Top achievements
Rank 1
Petya
Telerik team
Share this question
or