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

How To Replace Text and maintain Styling?

4 Answers 381 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Bob asked on 05 Apr 2021, 02:10 PM

We have a custom Find/Replace dialog for our RadRichTextBox.  As part of the replacement of text, we have this code:

var startPos = Document.Selection.Ranges.First.StartPosition;
var endPos = Document.Selection.Ranges.First.EndPosition;
 
this.CurrRichTextBox.Document.CaretPosition.MoveToPosition(endPos);
 
this.CurrRichTextBox.Document.Selection.SetSelectionStart(startPos);
this.CurrRichTextBox.Document.Selection.AddSelectionEnd(endPos);
 
StyleDefinition style = new StyleDefinition();
style.CopyPropertiesFrom(this.CurrRichTextBox.CurrentEditingStyle);
 
this.CurrRichTextBox.Delete(false);
this.CurrRichTextBox.Document.Insert(s, style);

 

However, this code uses the deprecated Insert method, and this also causes the history to be cleared.  What is the now correct method for replacing text and maintaining the styling of the text?  Can you please provide a code example?  I have tried to search through documentation, but I could not find any examples.

Thanks, Bob

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 06 Apr 2021, 08:02 AM

Hi Bob,

You can use the following code to replace the obsoleted method: 

var editor = new RadDocumentEditor(radRichTextBox.Document);
var span = new Span(s);
 
editor.InsertInline(span);
editor.ChangeSpanStyle(style);

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
answered on 06 Apr 2021, 07:26 PM

Hello Dimitar,

The text is updated without losing history now, but unfortunately, the style is not being set.  I can see the style definition contains the changes from the replaced span (bold, italic in my test), but the new span does not reflect those styles and remains normal.   Please see attached properties for before (old word) and after (new word).  Please let me know if I can provide any more information.

Thanks,

Bob

0
Accepted
Dimitar
Telerik team
answered on 07 Apr 2021, 10:07 AM

Hi Bob,

Can you try the following and see if it works on your side: 

var editor = new RadDocumentEditor(radRichTextBox.Document);
var span = new Span(s);
span.CopyPropertiesFromStyle(style);

editor.InsertInline(span);

I am looking forward to your reply.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
answered on 07 Apr 2021, 05:03 PM
Dimitar, this works perfect, thank you!
Tags
RichTextBox
Asked by
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Answers by
Dimitar
Telerik team
Bob
Top achievements
Rank 3
Iron
Iron
Veteran
Share this question
or