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

how to monitor keyboard combination key events? such as Ctrl+S

1 Answer 73 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
zj
Top achievements
Rank 1
zj asked on 27 Mar 2018, 01:58 AM

Hi,
The requirement is to monitor the Ctrl+S event, then Overwrite the dialog of selection file and give the default path and filename.

How to solve this?
Thanks!

1 Answer, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 29 Mar 2018, 08:14 AM
Hi,

To obtain the Save File dialog that is opened when the users try to save a file, you will need to customize the command that opens it. In the CommandExecuting event of RadRichTextBox, you can check whether the current command is SaveCommand and rewrite it so you can modify the properties of the dialog. Following is an example implementation of this approach:

private void RadRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
    if (e.Command is SaveCommand)
    {
        e.Cancel = true;
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.InitialDirectory = @"D:\TestDocuments";
        var formatProviders = DocumentFormatProvidersManager.FormatProviders.Where(fp => fp.CanExport);
        saveDialog.Filter = string.Join("|", formatProviders.Select(fp => GetFilter(fp)).ToArray()) + "|All Files|*.*";
 
 
        bool? dialogResult = saveDialog.ShowDialog();
        if (dialogResult == true)
        {
            var extension = System.IO.Path.GetExtension(saveDialog.SafeFileName);
            var output = saveDialog.OpenFile();
            IDocumentFormatProvider provider = DocumentFormatProvidersManager.GetProviderByExtension(extension);
 
            using (output)
            {
                provider.Export(this.radRichTextBox.Document, output);
            }
        }
    }
}
 
public static string GetFilter(IDocumentFormatProvider formatProvider)
{
    return
        formatProvider.FilesDescription +
        " (" +
        string.Join(", ", formatProvider.SupportedExtensions.Select(ext => "*" + ext).ToArray()) +
        ")|" +
        string.Join(";", formatProvider.SupportedExtensions.Select(ext => "*" + ext).ToArray());
}

I have just logged a task to expose an API that would allow achieving similar customizations easily. You can vote for the task and subscribe to track its status using the related public item on our feedback portal.

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
zj
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or