ChangeParagraphTextAlignment is over-eager to change alignment

4 posts, 1 answers
  1. Mihajlo
    Mihajlo avatar
    47 posts
    Member since:
    Aug 2018

    Posted 24 Apr Link to this post

    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.

     

     

  2. Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 27 Apr Link to this post

    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.
  3. Mihajlo
    Mihajlo avatar
    47 posts
    Member since:
    Aug 2018

    Posted 27 Apr in reply to Dimitar Link to this post

    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;
    }
  4. Answer
    Dimitar
    Admin
    Dimitar avatar
    2908 posts

    Posted 28 Apr Link to this post

    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.
Back to Top