How to restore fore color to default (black) in RadRichTextEditor after the InsertReadOnlyRange (C#)

1 Answer 108 Views
RichTextEditor
Herbert
Top achievements
Rank 1
Herbert asked on 20 Sep 2022, 12:01 PM

Hi,

Let me describe my use case in these following steps:

  • created a RadRichTextEditor 
  • set the RadRichTextEditor text to "This is a sample"
  • set the cursor before the letter "s" from the word "sample"
  • created a readonly span and set the fore color to Yellow
  • inserted the readonly span "Inserted text" before the word "sample"

Please see the output from the attachment: InsertTextOnCurrentCaretPosition_UI_1.0.PNG

Code below, is the method of insertion of readonly span

public void InsertTextOnCurrentCaretPosition()
{
	// Creating and setting span
	Span readOnlyContent = new Span("Inserted text");
	readOnlyContent.ForeColor = Color.FromRgb(255, 255, 0);     // Yellow

	// Get the position of the current caret
	DocumentPosition caretPosition = new DocumentPosition(radRichTextEditor.Document.CaretPosition);
	caretPosition.AnchorToCurrentBoxIndex();

	// Insert the span
	radRichTextEditor.Document.Selection.Clear();
	radRichTextEditor.InsertInline(readOnlyContent);

	// Get the position of the current caret after inserting the span
	caretPosition.RestorePositionFromBoxIndex();
	DocumentPosition endCitation = radRichTextEditor.Document.CaretPosition;

	// Select the span by caret positions and insert as read only
	radRichTextEditor.Document.Selection.AddSelectionStart(caretPosition);
	radRichTextEditor.Document.Selection.AddSelectionEnd(endCitation);
	radRichTextEditor.InsertReadOnlyRange();

	radRichTextEditor.Focus();
}


After the insertion of the readonly span, I key-in 3 letter "a" and expected the fore color would be Black, but it seems it retained the color (Yellow) of the readonly span. As you can see on this attachment: InsertTextOnCurrentCaretPosition_UI_1.1.PNG

Is there a way to set/reset the fore color to Black after the readonly span? so that the key-in values would be in color Black

Code below, I tried setting the fore color to Black right after the InsertReadOnlyRange, but it changes the fore color of the 2nd word "text" of the readonly span as you can see on this attachment: InsertTextOnCurrentCaretPosition_UI_2.0.PNG

public void InsertTextOnCurrentCaretPosition()
{
	// Creating and setting span
	Span readOnlyContent = new Span("Inserted text");
	readOnlyContent.ForeColor = Color.FromRgb(255, 255, 0);     // Yellow

	// Get the position of the current caret
	DocumentPosition caretPosition = new DocumentPosition(radRichTextEditor.Document.CaretPosition);
	caretPosition.AnchorToCurrentBoxIndex();

	// Insert the span
	radRichTextEditor.Document.Selection.Clear();
	radRichTextEditor.InsertInline(readOnlyContent);

	// Get the position of the current caret after inserting the span
	caretPosition.RestorePositionFromBoxIndex();
	DocumentPosition endCitation = radRichTextEditor.Document.CaretPosition;

	// Select the span by caret positions and insert as read only
	radRichTextEditor.Document.Selection.AddSelectionStart(caretPosition);
	radRichTextEditor.Document.Selection.AddSelectionEnd(endCitation);
	radRichTextEditor.InsertReadOnlyRange();

	// Change the color of the editor back to black
	radRichTextEditor.Document.Selection.Clear();
	radRichTextEditor.ChangeTextForeColor(Color.FromRgb(0, 0, 0));   // Black
	radRichTextEditor.DocumentInheritsDefaultStyleSettings = true;
	radRichTextEditor.Focus();
}

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 22 Sep 2022, 08:30 AM

Hello Herbert,

After using your method you can change the current color with the following code: 

private void radButton1_Click(object sender, EventArgs e)
{
	InsertTextOnCurrentCaretPosition();

	this.radRichTextEditor.ChangeTextForeColor(Color.Black);
}

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Herbert
Top achievements
Rank 1
commented on 22 Sep 2022, 09:36 AM

Hi Dimitar,

In this use case, I have a preset value in the radRichTextEditor and inserted the readonlyspan (was set to color Yellow) before the "sample" word. 

Then the color of the last word of the readonlyspan, in this case is the "text" word, was changed to Black.

You can see the output in this attachment: InsertTextOnCurrentCaretPosition_UI_2.0.PNG

Thanks

Dimitar
Telerik team
commented on 22 Sep 2022, 11:19 AM

Hi,

The mistake is mine. I have checked this again and the possible solution is to add a space to the inserted span or add a new span with a single space. 

I hope this helps. Should you have any other questions do not hesitate to ask.
Herbert
Top achievements
Rank 1
commented on 27 Sep 2022, 02:40 AM

Hi,

It worked, thanks.

Just to confirm, adding space after the inserted span is just a work around? or is there a way to to set the font color back to Black after the inserted span without affecting the color of the span and without the extra space?

Nikolay Demirev
Telerik team
commented on 27 Sep 2022, 02:27 PM

Hi Herbert,

Adding space is not a workaround, this is how MS Word works. We are trying to mimic its behavior in the RichTextEditor.

Another way to change the color is to insert some text, then select it and apply the new color, after that to return the caret where you want it to be placed.

Tags
RichTextEditor
Asked by
Herbert
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or