How to limit RichTextEditor to 1 page

5 posts, 1 answers
  1. Laurent
    Laurent avatar
    3 posts
    Member since:
    Aug 2016

    Posted 23 Aug Link to this post

    Hi,

     

    I am looking for a way to limit the RichTextEditor to only one page.

    The user must not be able to enter more text than can fit one a single page.

     

    Thanks in advance!

    Laurent

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 24 Aug Link to this post

    Hello Laurent,

    Thank you for writing. 

    There is not a built-in method for limiting the user to 1 page. However, note that you can handle the user input by subscribing to the CommandExecuting event and if the current page index is greater than 1 you can cancel the event. Here is a sample code snippet for handling text input:
    public Form1()
    {
        InitializeComponent();
     
        this.radRichTextEditor1.LayoutMode = DocumentLayoutMode.Paged;         
        this.radRichTextEditor1.CommandExecuting += radRichTextEditor1_CommandExecuting;
    }
     
    private void radRichTextEditor1_CommandExecuting(object sender, Telerik.WinForms.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
    {
        if (e.Command is InsertTextCommand)
        {
            var index = (this.radRichTextEditor1.RichTextBoxElement.ActiveEditorPresenter as DocumentPrintLayoutPresenter).GetCurrentPage();
            if (index > 1)
            {
                e.Cancel = true;
            }
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Laurent
    Laurent avatar
    3 posts
    Member since:
    Aug 2016

    Posted 24 Aug in reply to Dess Link to this post

    Hello Dess,

    thank you for your fast response.

    Unfortunately, the solution you posted will not work in all cases. If the user inserts content on the first page it can occur that the following content is pushed to the second page.Your solution does not address these cases.

    Is there any event which is fired when content breaks to a new page? Any other solutions?

     

    Regards,

    Laurent

  5. Answer
    Dess
    Admin
    Dess avatar
    1609 posts

    Posted 25 Aug Link to this post

    Hello Laurent, 

    Thank you for writing back. 

    As it was already noted in my previous post, there is not a specific event that is fired when the content jumps to a new page.

    RadRichTextEditor uses different commands for manipulating the document. Hence, the CommandExecuting event is fired for different types of commands. It is up to you which commands to cancel when pages number is increased. However, some of the insert commands, e.g. InsertPictureCommand, are triggered before the image is actually inserted in the document.

    An alternative solution that I can suggest is to subscribe to the CommandExecuted event and make undo of the last insert operation that exceeds the pages limit: 
    private void radRichTextEditor1_CommandExecuted(object sender, CommandExecutedEventArgs e)
    {
         if (this.radRichTextEditor1.Document.FirstLayoutBox.ChildLayoutBoxes.Count > 1)
             {
                    this.radRichTextEditor1.Undo();
                    MessageBox.Show("The document must be one page.");
             }
    }

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. Laurent
    Laurent avatar
    3 posts
    Member since:
    Aug 2016

    Posted 25 Aug in reply to Dess Link to this post

    Great!

    This was exactly what I was looking for!

    Thank you for your help!

Back to Top
UI for WinForms is Visual Studio 2017 Ready