How to issue NewDocument command in code

2 posts, 1 answers
  1. Kenneth
    Kenneth avatar
    100 posts
    Member since:
    Apr 2014

    Posted 31 Aug 2014 Link to this post

    I want to take over the 'New' command of the RibbonUI to allow the user to save any changes made to the current document first.

    How can I invoke the NewDocumentCommad from code?

    I assume I could also simply create a new document and use it to replace the current document, but there may be times when I want to call on the already implemented commands form code.
  2. Answer
    Petya avatar
    973 posts

    Posted 02 Sep 2014 Link to this post

    Hello Kenneth,

    Invoking a command from code is pretty straight-forward, you just need to access the respective command through RadRichTextBox's Commands property and call its Execute() method:

    If you want to trigger additional actions prior or after a specific command is executed, you can subscribe to the CommandExecuting and CommandExecuted events of the control respectively. For example, the following snippet warns the user that they are about to create a new document and allows them to save their changes:
    void radRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
        if (e.Command is NewDocumentCommand)
            MessageBoxButton btnMessageBox = MessageBoxButton.YesNoCancel;
            MessageBoxImage icnMessageBox = MessageBoxImage.Warning;
            MessageBoxResult rsltMessageBox = MessageBox.Show("Save your changes before creating a new document?", "Save Changes", btnMessageBox, icnMessageBox);
            switch (rsltMessageBox)
                case MessageBoxResult.Yes:
                        e.Cancel = true;
                        (sender as RadRichTextBox).Commands.SaveCommand.Execute();
                case MessageBoxResult.No:
                case MessageBoxResult.Cancel:

    I hope you find this useful.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. DevCraft banner
Back to Top