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

Selection Exception

7 Answers 109 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Ivan
Top achievements
Rank 1
Ivan asked on 19 Dec 2017, 08:38 AM
Hi! Sometimes when I select some text in TextBox I get next error:

System.ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length
   at System.String.Substring(Int32 startIndex, Int32 length)
   at Telerik.Windows.Documents.Layout.SpanLayoutBox.BinarySearchSplitIndex(SpanLayoutBox spanBox, Single availableWidth)
   at Telerik.Windows.Documents.SpanBoxPositionHandler.MoveToLocation(PointF location)
   at Telerik.Windows.Documents.DocumentPosition.SetPosition(PointF position, Boolean moveToNextIfOutOfBox)
   at Telerik.Windows.Documents.Selection.MouseSelectionHandler.UpdateSelectionAndCaretPosition()
   at Telerik.Windows.Documents.Selection.MouseSelectionHandler.RegisterDocumentMouseMove(Point position, SourceType source)
   at Telerik.Windows.Documents.UI.DocumentPresenterBase.HandleMouseMoveOnPosition(Point position, SourceType source)
   at Telerik.Windows.Documents.UI.DocumentPresenterBase.Owner_MouseMove(Object sender, MouseEventArgs e)

How I can catch this error for handling?

7 Answers, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 21 Dec 2017, 03:46 PM
Hi Ivan,

The error you are getting is pretty strange and we are not sure why it might occur. Is it possible to share an example document along with steps to reproduce the error so we can investigate it locally and try to propose a solution for the case?

Please, note that in public forums you can attach only images. If you would like, you could open a support ticket where you can attach archive files.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Renan
Top achievements
Rank 1
answered on 13 Apr 2018, 05:01 PM

Sometimes I am getting the same exception. Usually when I am selecting the content with text and image after paste and the cursor goes out of the Richtext.

I am using a custom paste because  I am resizing an image when paste.

 

CommandExecuting += (s, e) =>
{

  if (e.Command is PasteCommand)
                {
                    var fragment = ClipboardEx.GetDocument();
                    if (fragment != null)
                    {
                        var document = fragment.ToDocument();

                        if (ImageProcessor.InsertImageOnPaste(document)) //just return true or false to check if it is a new image.
                        {
                            if (document.Sections.First.Blocks != null && document.Sections.Any() &&
                                document.Sections.First.Blocks.First != null)
                            {
                                 
                                var paragraphs = document.Sections.First.Blocks.OfType<TModel.Paragraph>();

                                var caretPosition = Document.CaretPosition.GetCurrentParagraphBox().AssociatedParagraph;

                                foreach (var paragraph in paragraphs)
                                {
                                    var copiedParagraph = new TModel.Paragraph();

                                    if (caretPosition?.Parent is TModel.Section section)
                                    {
                                        copiedParagraph = paragraph.CreateDeepCopy() as TModel.Paragraph;
                                        section.Blocks.AddAfter(caretPosition, copiedParagraph);
                                        caretPosition = copiedParagraph;
                                    }
                                }
                            }

                            UpdateEditorLayout();
                            e.Cancel = true;
                           
                        }
                    }

}

0
Tanya
Telerik team
answered on 18 Apr 2018, 02:20 PM
Hello Renan,

We additionally investigated the case and seems like a similar error could be thrown if there is a Span object with an empty text string in the document. These elements are not valid according to RadDocument's structure, so I would like to ask you to check if the document contains such and remove them if so.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Renan
Top achievements
Rank 1
answered on 18 Apr 2018, 03:17 PM

Hi Tanya, Thank's for your reply.
Instead of adding a new Block, I am updating my fragment document from Clipboard.

if (e.Command is PasteCommand)
                {
                    var fragment = ClipboardEx.GetDocument();
                    if (fragment != null)
                    {
                        var document = fragment.ToDocument();
                        var inlines = document.Sections.First.Blocks;
                        var imageInlines = inlines?.SelectMany(m => m.EnumerateChildrenOfType<TModel.ImageInline>()).ToList();
                        if (imageInlines != null && imageInlines.Any())
                        {
                            var newDoc = ImageProcessor.ResizeImageOnPaste(document);
                            if (newDoc != null)
                            {
                                var newFragment = new TModel.DocumentFragment(newDoc); //create a new fragment with the new image.
                                ClipboardEx.SetDocument(newFragment); //update document.
                            }
                            else
                            {
                                e.Cancel = true;
                            }
                        }
                    }
                }

0
Tanya
Telerik team
answered on 23 Apr 2018, 12:52 PM
Hi Renan,

The invalid content could be already in the document or it could be constructed while inserting the fragment. This is why I would like to ask you to check if there is a Span with an empty text content. If such is present, the workaround could be to delete it. However, in case this is missing, it would be of a great help for us if you can provide sample documents (the one that is already in RadRichTextBox and the one that is pasted inside) and steps to reproduce so we can test the issue locally and find what is causing it.

You can also open a support ticket, where you can attach archives so we can easily exchange files.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Renan
Top achievements
Rank 1
answered on 23 Apr 2018, 03:08 PM

Hi Tanya,

Yes, there is a span with an empty space.

0
Tanya
Telerik team
answered on 26 Apr 2018, 11:26 AM
Hi Renan,

If the span contains just a single space, that shouldn't be an issue. However, if it is empty - that might be causing the exception. Could you please elaborate more on what is the empty space? If the string.IsNullOrEmpty() method returns true when invoked with the Span.Text value, removing this span could prevent the issue you are observing. Could you please test that? Here is an example in code:

foreach (var span in this.radRichTextBox.Document.EnumerateChildrenOfType<Span>())
{
    if (string.IsNullOrEmpty(span.Text))
    {
        (span.Parent as Paragraph).Inlines.Remove(span);
    }
}

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
Ivan
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Renan
Top achievements
Rank 1
Share this question
or