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

How to issue NewDocument command in code

1 Answer 60 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Kenneth
Top achievements
Rank 2
Iron
Kenneth asked on 31 Aug 2014, 01:51 PM
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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Petya
Telerik team
answered on 02 Sep 2014, 03:45 PM
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:
this.radRichTextBox.Commands.NewDocumentCommand.Execute();

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();
                    break;
                }
            case MessageBoxResult.No:
            case MessageBoxResult.Cancel:
                break;
        }
    }
}

I hope you find this useful.

Regards,
Petya
Telerik
 
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.
 
Tags
RichTextBox
Asked by
Kenneth
Top achievements
Rank 2
Iron
Answers by
Petya
Telerik team
Share this question
or