locked
Search text within a RichTextBlock RRS feed

  • Question

  • Hi.

    I would like some suggestions about how to search for text inside a RichTextBlock. My current method is not very optimal and it is causing me a lot of problems.

    For example, if I want to search for the word "green", I do a SelectAll() (in my RichTextBlock)  followed by a SelectedText() and from the returned string, I search for the word. Then, I set two TextPointer to the position of the word in the RichTextBlock, using Select(TextPointer start, TextPointer end).

    //Pseudocode

    MyRTB.SelectAll(); string SearchInThisString = MyRTB.SelectedText(); FindWordPosition(string searchThis); //Find the position of the string and the length and assign them to two TextPointers. MyRTB.Select(TextPointerStart, TextPointerEnd);


    My problem is that the Selected area from the RTB is wrong, it is always off (sometimes by 1-5 indexes or by a lot).

    My RTB has different Paragraphs/Runs which sometimes are bold/italic, maybe thats related to my problem.

    Thanks.

    EDIT: I am trying to do something similar to the search functionality of the Wikipedia app.


    Friday, March 8, 2013 11:34 PM

All replies

  • Hi Juande,

    Your general concept sounds correct.

    The first thing I'd do would be to simplify the test case to make sure FindWordPosition works correctly on a simple run and then work up from there.

    If you need further help with this it would be easier if you could post a more complete code snippet if not upload a minimal sample demonstrating what you are doing.

    --Rob

    Saturday, March 9, 2013 3:36 AM
    Moderator
  • Hi Juande,

    Your general concept sounds correct.

    The first thing I'd do would be to simplify the test case to make sure FindWordPosition works correctly on a simple run and then work up from there.

    If you need further help with this it would be easier if you could post a more complete code snippet if not upload a minimal sample demonstrating what you are doing.

    --Rob

    I'll post the complete code next Monday, because I do not have it with me right now. Thanks for the reply.
    Saturday, March 9, 2013 5:09 PM