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

5 posts, 0 answers
  1. JMSteele
    JMSteele avatar
    3 posts
    Member since:
    Feb 2007

    Posted 27 Jun 2013 Link to this post

    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.
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 01 Jul 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. JMSteele
    JMSteele avatar
    3 posts
    Member since:
    Feb 2007

    Posted 01 Jul 2013 Link to this post

    Thanks for such a detailed response, Iva. Very helpful.
  5. Jaypee
    Jaypee avatar
    13 posts
    Member since:
    Jun 2013

    Posted 23 Jul 2013 Link to this post

    how if i dont want to use regular epression. can i disable "use regular epression"?
  6. Petya
    Admin
    Petya avatar
    975 posts

    Posted 25 Jul 2013 Link to this post

    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 >>
Back to Top
DevCraft banner