Keep formating in a richtexteditor when correcting text

1 Answer 40 Views
RichTextEditor
Eva
Top achievements
Rank 1
Eva asked on 17 Sep 2024, 08:12 AM

Hello 

I am trying to fix some text when it is being inserted in a richtexteditor but each time I move the caret position to document end the font changes back to default. This is my code:

 if (txtCorrections.Text[txtCorrections.Text.Length - 1] == ')' && txtCorrections.Text[txtCorrections.Text.Length - 2] == ' ')
                {

                    txtCorrections.Text = txtCorrections.Text.Remove(txtCorrections.Text.Length - 2, 1);
                    txtCorrections.Document.CaretPosition.MoveToDocumentEnd();


                }
                I use this code in a textChanged.

Thank you for help.

                         

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 19 Sep 2024, 01:00 PM

Hi Eva,

I appreciate the provided code snippet. Setting the Text property will reset the text in the control with the font. If I have correctly understood your scenario you want to replace a specific string combination. I would suggest using the ReplaceAllMatches approach mentioned in the Search help article in our documentation. Here is what I have in mind. 

private void RadRichTextEditor1_TextChanged(object sender, EventArgs e)
{
    if (radRichTextEditor1.Text[radRichTextEditor1.Text.Length - 1] == ')' && radRichTextEditor1.Text[radRichTextEditor1.Text.Length - 2] == ' ')
    {
        ReplaceAllMatches(" )", ")");
    }
}

private void ReplaceAllMatches(string toSearch, string toReplaceWith)
{
    toSearch = Regex.Escape(toSearch);
    this.radRichTextEditor1.Document.Selection.Clear(); // this clears the selection before processing
    Telerik.WinForms.Documents.TextSearch.DocumentTextSearch search = new Telerik.WinForms.Documents.TextSearch.DocumentTextSearch(this.radRichTextEditor1.Document);
    List<Telerik.WinForms.Documents.TextSearch.TextRange> rangesTrackingDocumentChanges = new List<Telerik.WinForms.Documents.TextSearch.TextRange>();
    foreach (var textRange in search.FindAll(toSearch))
    {
        Telerik.WinForms.Documents.TextSearch.TextRange newRange = new Telerik.WinForms.Documents.TextSearch.TextRange(new Telerik.WinForms.Documents.DocumentPosition(textRange.StartPosition, true), new Telerik.WinForms.Documents.DocumentPosition(textRange.EndPosition, true));
        rangesTrackingDocumentChanges.Add(newRange);
    }
    foreach (var textRange in rangesTrackingDocumentChanges)
    {
        this.radRichTextEditor1.Document.Selection.AddSelectionStart(textRange.StartPosition);
        this.radRichTextEditor1.Document.Selection.AddSelectionEnd(textRange.EndPosition);
        this.radRichTextEditor1.Insert(toReplaceWith);
        textRange.StartPosition.Dispose();
        textRange.EndPosition.Dispose();
    }
}

This way the text will be replaced without breaking the applied font style. I hope that this approach will work for you.

Regards,
Dinko | Tech Support Engineer
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.

Tags
RichTextEditor
Asked by
Eva
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or