RAD Studio |  |

Changing the Double-click Behavior

When the component is double-clicked, the Edit method of the component editor is called. By default, the Edit method executes the first command added to the context menu. Thus, in the previous example, double-clicking the component executes the DoThis command.

While executing the first command is usually a good idea, you may want to change this default behavior. For example, you can provide an alternate behavior if

- you are not adding any commands to the context menu.
- you want to display a dialog that combines several commands when the component is double-clicked.

Override the Edit method to specify a new behavior when the component is double-clicked. For example, the following Edit method brings up a font dialog when the user double-clicks the component:
Copy Code

    procedure TMyEditor.Edit;
    var
      FontDlg: TFontDialog;
    begin
      FontDlg := TFontDialog.Create(Application);
      try
        if FontDlg.Execute then
          MyComponent.FFont.Assign(FontDlg.Font);
      finally
        FontDlg.Free
      end;
    end;

Copy Code

    void __fastcall TMyEditor::Edit(void)
    {
    TFontDialog *pFontDlg = new TFontDialog(NULL);
    pFontDlg->Execute();
    ((TMyComponent *)Component)->Font = pFontDlg->Font;
    delete pFontDlg;
    }

Note: If you want a double-click on the component to display the Code editor for an event handler, use TDefaultEditor as a base class for your component editor instead of TComponentEditor. Then, instead of overriding the Edit method, override the protected TDefaultEditor.EditProperty method instead. EditProperty scans through the event handlers of the component, and brings up the first one it finds. You can change this to look a particular event instead. For example:
Copy Code

    procedure TMyEditor.EditProperty(PropertyEditor: TPropertyEditor;
      Continue, FreeEditor: Boolean)
    begin
      if (PropertyEditor.ClassName = 'TMethodProperty') and
        (PropertyEditor.GetName = 'OnSpecialEvent') then
        // DefaultEditor.EditProperty(PropertyEditor, Continue, FreeEditor);
    end;

Copy Code

    void __fastcall TMyEditor::EditProperty(TPropertyEditor* PropertyEditor,
    bool &Continue, bool &FreeEditor)
    {
    if (PropertyEditor->ClassNameIs("TMethodProperty") &&
    CompareText(PropertyEditor->GetName, "OnSpecialEvent") == 0)
    {
    TDefaultEditor::EditProperty(PropertyEditor, Continue, FreeEditor);
    }
    }

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!