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
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
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;
}
}
}
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
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;
}
}
}
}
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
Hi Tanya,
Yes, there is a span with an empty space.
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