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

Get bookmarks in selection?

3 Answers 45 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 13 Feb 2012, 09:23 PM
Hi

I've tried using inspiration from a Silverlight question, but to no avail (http://www.telerik.com/community/forums/silverlight/richtextbox/get-hyperlink-from-selected-text.aspx)

What I'm trying to do is find any bookmarks within the current selection, or that surround the current caret position.  I'd settle for just the last, but I'm struggling!

e.g, say I had a bookmark where the "[" and "]" are in the following sentence:

"The quick brown fox [jumped over] the lazy [dog]"

If the caret was anywhere within jumped over (either no text selected, or anywhere up to all of "jumped over" selected), I'd be able to get the bookmark name of the surrounding bookmark.

If I selected the whole sentence, I'd hope to get both bookmarks returned - but the first part is more important!

Thanks for any help.

Charlie

3 Answers, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 17 Feb 2012, 01:56 PM
Hi Charlie

It is possible to achieve both tasks.
If I have understood you correctly, the desired behavior is:
1. If there is no selection, the containing bookmark ranges should be returned;
2. If there is selection, a bookmark range should be returned only if both the BookmarkRangeStart and BookmarkRangeEnd are selected.

Here is a method that does this task:

    private List<BookmarkRangeStart> GetAllBookmarks()
    {
        List<BookmarkRangeStart> bookmarks = new List<BookmarkRangeStart>();
        if (this.editor.Document.Selection.IsEmpty)
        {
            InlineLayoutBox currentInline = this.editor.Document.CaretPosition.GetCurrentInlineBox();
            bookmarks = this.editor.Document.GetContainingAnnotationRanges<BookmarkRangeStart>(currentInline.AssociatedInline, true).ToList<BookmarkRangeStart>();
        }
        else
        {
            IEnumerable<InlineLayoutBox> selectedBoxes = this.editor.Document.Selection.GetSelectedBoxes();
            foreach (var inlineBox in selectedBoxes)
            {
                BookmarkRangeStart bookmarkStart = inlineBox.AssociatedInline as BookmarkRangeStart;
                if (bookmarkStart != null && selectedBoxes.Contains(bookmarkStart.End.FirstLayoutBox as InlineLayoutBox))
                {
                    bookmarks.Add(bookmarkStart);
                }
            }
        }
        return bookmarks;
    }
}

Note that if you select only the text in the bookmark, the BookmarkRangeStart will be considered outside of the selection and will not be included in the bookmarks collection. If this is not desired, you can manually check if there is an inline before the fist layout box in the selected boxes and if it is a BookmarkRangeStart answering the other requirements.

I hope this answers your questions. Don't hesitate to contact us again in case of a problem.

Greetings,
Martin
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Charles
Top achievements
Rank 1
answered on 17 Feb 2012, 02:00 PM
Hi Martin

Thanks for this - I'll give it a try.

I had come up with a solution that works, but it's not quite as elegant as yours - taking a while to get to grips with the document model!
Charlie
0
Ivailo Karamanolev
Telerik team
answered on 20 Feb 2012, 01:41 PM
Hello,

Let us know if you need additional assistance on the matter.

Greetings,
Ivailo Karamanolev
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
RichTextBox
Asked by
Charles
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Charles
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Share this question
or