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

WPF RadRichTextBox - Insert AnnotationRanges using InsertFragment throws exception when Track Changes is ON

1 Answer 86 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Babu
Top achievements
Rank 1
Babu asked on 15 Dec 2014, 10:41 AM
In WPF RadRichTextBox, we try to Insert AnnotationRanges using InsertFragment method. But, it throws exception in some scenarios when Track Changes is ON. Please find sample code below.

try
            {
                //Initialize
                RadDocument associatedDocument = associatedRichTextBox.Document;
                DocumentSelection selection = associatedDocument.Selection;
                DocumentPosition documentPosition = associatedDocument.CaretPosition;

                string selectedText = GetSelectedText(associatedDocument);

                // If text is not selected 
                if (String.IsNullOrEmpty(selectedText) && String.IsNullOrWhiteSpace(selectedText))
                {
                    MessageBox.Show("Please select a text", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    RadDocument tempDocument = associatedDocument.Selection.CreateDocumentFromSelection();

                    IEnumerable<FormulaRangeStart> formulaMarkers = tempDocument.GetAnnotationMarkersOfType<FormulaRangeStart>();

                    bool bformulaRangeExistsInSelection = false;
                    foreach (FormulaRangeStart frmla in formulaMarkers)
                    {
                        bformulaRangeExistsInSelection = true;
                        break;
                    }

                    if (bformulaRangeExistsInSelection)
                    {
                        MessageBox.Show("Selection already contains a formula range", "Formula Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }

                    FormulaRangeStart formulaRangeStart = new FormulaRangeStart();
                    FormulaRangeEnd formulaRangeEnd = new FormulaRangeEnd();
                    formulaRangeEnd.PairWithStart(formulaRangeStart);

                    tempDocument.CaretPosition.MoveToFirstPositionInDocument();
                    tempDocument.InsertInline(formulaRangeStart);

                    tempDocument.CaretPosition.MoveToLastPositionInDocument();
                    tempDocument.InsertInline(formulaRangeEnd);

                    tempDocument.Selection.SelectAll();

                    IEnumerable<Paragraph> paragraphs = tempDocument.Selection.GetSelectedParagraphs();

                    foreach (Paragraph paragraph in paragraphs)
                    {
                        foreach (Telerik.Windows.Documents.Model.Inline inline in paragraph.Inlines)
                        {
                            if (inline is Span)
                            {
                                ((Span)(inline)).HighlightColor = Colors.DarkGray;
                            }
                        }
                    }

                    tempDocument.CaretPosition.MoveToLastPositionInDocument();

                    Span spanResetBgColor = new Span(FormattingSymbolLayoutBox.SPACE_SYMBOL_VISUAL);
                    spanResetBgColor.HighlightColor = Colors.White;
                    tempDocument.InsertInline(spanResetBgColor);

                    tempDocument.Selection.SelectAll();

                    associatedRichTextBox.IsTrackChangesEnabled = true;
                    associatedRichTextBox.TrackChangesOptions.TrackFormatting = true;

                    associatedRichTextBox.InsertFragment(tempDocument.Selection.CopySelectedDocumentElements(true));
                    associatedRichTextBox.Insert(FormattingSymbolLayoutBox.ENTER);
                    associatedRichTextBox.Document.Selection.Clear();
                    associatedRichTextBox.UpdateLayout();
                    associatedRichTextBox.UpdateEditorLayout();
                    associatedRichTextBox.Focus();
                    if (associatedRichTextBox.ActiveEditorPresenter != null)
                    {
                        associatedRichTextBox.ActiveEditorPresenter.RecreateUI();
                    }
                }

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 18 Dec 2014, 08:25 AM
Hi Babu,

I've tried to reproduce the issue you described, but to no avail. I've tested the scenario with ReadOnlyRanges instead of "FormulaRanges". It will be great if you can isolate the issue in a sample demo project and send it to us. In this way we can be much more effective in our try to find what causes the issue. It will be also of our great help if you share with us what is the exception, are there any specific circumstances when it occurs as you mentioned the exception is thrown in "some scenarios". Which are these scenarios and can you provide steps to reproduce?

I'm looking forward to your reply.

Regards,
Todor
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
RichTextBox
Asked by
Babu
Top achievements
Rank 1
Answers by
Todor
Telerik team
Share this question
or