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

ArgumentOutOfRangeException when calling ChangeTextForeColor

2 Answers 72 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Harry
Top achievements
Rank 1
Harry asked on 13 Jun 2011, 12:38 PM
Hi,

I am programmatically selecting text and changing the fore colour. I believe this is done through the RadRichTextBox.ChangeTextForeColor() which changes the colours for all text that is selected.

So my process is:

1. Save existing selections
2. Add selections for the text I want to colour using Document.Selections.AddSelectionStart/End
3. Call ChangeTextForeColor(Colors.Red)
4. Revert previous selections

When I call this the first time it works correctly; however, I also want to revert the colours at specific point, and when I do this using ChangeTextForeColor(Colors.Black) I get an ArgumentOutOfRangeException on "startIndex cannot be larger than length of string"


Thanks

2 Answers, 1 is accepted

Sort by
0
Harry
Top achievements
Rank 1
answered on 14 Jun 2011, 11:10 AM
The issue seems to be because changing the colours creates a new span, and the associated DocumentPositions I have for the text gets changed as well according to the span.

I have since changed my approach for this problem. I am highlighting the text as normal, but I am now un-highlighting by enumerating through the Spans using Document.EnumerateChildrenOfType<Span>() and changing the ForeColor/HighlightColor when I need to.

I have two questions with this method:

1. The spans are still being created, is this going to cause a problem performance wise? After I undo I'd ideally like to "merge" the spans back to how they were before since they're the same.
2. When I change the ForeColor of a Span the colour isn't actually changed until some event is fired. I'm not sure which, but moving the caret doesn't do it; so far it's only on a KeyDown event. How do I programmatically call this?

Thanks.
0
Iva Toteva
Telerik team
answered on 16 Jun 2011, 04:30 PM
Hi Harry,

Adjacent spans which have equal properties are merged when the method MergeSpansWithSameStyles from RadDocument's API is invoked. This occurs when the document is measured. The document is measured by Silverlight when it is shown in the editor, but you can also measure it explicitly in the following way:

document.Measure(RadDocument.MAX_DOCUMENT_SIZE);

Overall, the spans will be merged even if you don't take any special care of them.

The new properties of the spans are not applied until the layout of the editor is updated. You can invoke the following method to force the changes:
this.editor.UpdateEditorLayout();

If you share some more details on your scenario and implementation, we may be able to come up with more appropriate and optimized solutions to achieve the desired result.

Greetings,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Harry
Top achievements
Rank 1
Answers by
Harry
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or