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

Adding new command that needs access out of RichTextBox

1 Answer 65 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Mihajlo
Top achievements
Rank 1
Iron
Mihajlo asked on 05 Feb 2019, 12:49 PM

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? 

1 Answer, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 06 Feb 2019, 07:49 AM
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.
Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Iron
Answers by
Dimitar
Telerik team
Share this question
or