Adding new command that needs access out of RichTextBox

2 posts, 1 answers
  1. Mihajlo
    Mihajlo avatar
    38 posts
    Member since:
    Aug 2018

    Posted 05 Feb Link to this post

    When we create a new command (inherited from RichTextBoxCommandBase) we have the access to this.AssociatedRichTextBox. But I need the access to properties of the form in which the editor and ribbon bar are just two more properties.

    One workaround would be to have empty ExecuteOverride method for the command, and handle the command in CommandExecuting handler of the form.

    Another workaround would be to pass the form's reference to custom ribbon bar, and then pass this reference further to the command on command creation.

    What is the difference between the two, what is the recommended way? 

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2677 posts

    Posted 06 Feb Link to this post

    Hi Mihajlo,

    In my opinion, the command should not be tied with the form in any way. If it is you will not be able to reuse it with other forms. The ExecuteOverride method has a single parameter and you should pass all the information needed for the command in it (perhaps you can create a custom object and store all needed inforation in it). For example, the ChangeFontFamilyCommand checks if the parameter is a valid font object or name and changes the font according to the parameter. Here is the code (taken from our source): 
    protected override void ExecuteOverride(object parameter)
    {
        FontFamily fontFamily = null;
     
        if (parameter is FontFamily)
        {
            fontFamily = parameter as FontFamily;
        }
        else if (parameter is string)
        {
            fontFamily = new FontFamily((string)parameter);
        }
        else if (parameter is FrameworkElement)// this is for backward compatibility with 2010 Q2 SP1 and previous
        {
            FrameworkElement frameworkElement = (FrameworkElement)parameter;
            fontFamily = new FontFamily(frameworkElement.Tag as string);
        }
     
        if (fontFamily != null)
        {
            this.AssociatedRichTextBox.ChangeFontFamily(fontFamily);
        }
    }

    I hope this helps. Should you have any other questions do not hesitate to ask.
     
    Regards,
    Dimitar
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top