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

ChangeParagraphTextAlignment is over-eager to change alignment

3 Answers 11 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Mihajlo
Top achievements
Rank 1
Mihajlo asked on 24 Apr 2020, 04:00 PM

In measured document of Word-inspired project we have to use IDocument interface. How do I add a new paragraph to document and set its text alignment without having to know the alignment of previous paragraph? ChangeParagraphTextAlignment method behaves differently based on the current paragraph alignment. If current is Center, and we "change" to Center the result is Left. If current is Left and we "change" to Left the result is Justify. The method really really wants to change the alignment.

So in my code I do InsertParagraph(), not knowing how the previous paragraph was aligned. How to center-align this new paragraph?

I suppose I can use ClearAllFormatting, but now here's the followup question. How to reuse all other paragraph properties from previous paragraph, like indentation and spacing, and just set text alignment to Center?

I could make two calls, "change" to Justify, and then to Center. Whatever was the starting state we should reach Center, but this looks like a hack to me. In case we want to set the alignment to Left we would have to make three calls to make sure we actually reach Left state (for instance "change" to Right, then Justify, then Left).I feel this should not be so complicated.

 

 

3 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 27 Apr 2020, 10:41 AM

Hi Mihajlo,

I have tested this and it seems to work on my side. Maybe I am missing something.. I have attached my test project could you please check it and let me know what I need to change in order to reproduce the undesired behavior.

Thank you in advance for your patience and cooperation. 

Regards,
Dimitar
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Mihajlo
Top achievements
Rank 1
answered on 27 Apr 2020, 11:08 AM

Project must be Word-inspired. Add the following code to CommandExecuting handler and press Ctrl+Enter repeatedly. Expected result is several centered paragraphs, and actual result is odd ones centered, and even ones left aligned.

if (e.Command is Telerik.WinForms.Documents.RichTextBoxCommands.InsertPageBreakCommand)
{
    var editor = this.radRichTextEditor1.DocumentEditor;
    editor.InsertParagraph();
    editor.Insert("New paragraph");
    editor.ChangeParagraphTextAlignment(Telerik.WinForms.Documents.Layout.RadTextAlignment.Center);
 
    e.Cancel = true;
}
0
Accepted
Dimitar
Telerik team
answered on 28 Apr 2020, 09:10 AM

Hello Mihajlo,

I have examined this and the editor works in a similar way as the Ribbon UI. If you have a paragraph that is already centered setting to Center will toggle it and it will switch to the default value which is left. In your case, you can get the new paragraph and check its current alignment:

var editor = this.radRichTextEditor1.DocumentEditor;
editor.InsertParagraph();
editor.Insert("New paragraph");
var p = radRichTextEditor1.Document.CaretPosition.GetCurrentParagraph(); 
if (p.TextAlignment != RadTextAlignment.Center)
{
    editor.ChangeParagraphTextAlignment(RadTextAlignment.Center);
}

Let me know how this works for you.

Regards,
Dimitar
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Mihajlo
Top achievements
Rank 1
Share this question
or