Selection Exception

8 posts, 0 answers
  1. Ivan
    Ivan avatar
    1 posts
    Member since:
    Dec 2017

    Posted 19 Dec 2017 Link to this post

    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?
  2. Tanya
    Admin
    Tanya avatar
    671 posts

    Posted 21 Dec 2017 Link to this post

    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.
  3. Renan
    Renan avatar
    3 posts
    Member since:
    Oct 2017

    Posted 13 Apr Link to this post

    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;
                               
                            }
                        }

    }

  4. Tanya
    Admin
    Tanya avatar
    671 posts

    Posted 18 Apr Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  5. Renan
    Renan avatar
    3 posts
    Member since:
    Oct 2017

    Posted 18 Apr in reply to Tanya Link to this post

    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;
                                }
                            }
                        }
                    }

  6. Tanya
    Admin
    Tanya avatar
    671 posts

    Posted 23 Apr Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
  7. Renan
    Renan avatar
    3 posts
    Member since:
    Oct 2017

    Posted 23 Apr in reply to Tanya Link to this post

    Hi Tanya,

    Yes, there is a span with an empty space.

  8. Tanya
    Admin
    Tanya avatar
    671 posts

    Posted 26 Apr Link to this post

    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 allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top