Can a Component Editor be executed on multiple components?

。_饼干妹妹 提交于 2019-12-11 02:54:16

问题


Short Version

I am trying to implement my first ever Component Editor for a custom button I have made. With the help of some online articles I have successfully installed the editor and can see the menu item when I right click on my button in the Form Designer.

But this component editor menu is not showing when selecting more than one of my button controls.

Do Component Editors only work with single selected controls by default, or can they work with multiple selected controls and if so how?

Long Version

I was in the process of implementing a TPropertyEditor for one of my own components but have now decided that a TComponentEditor would be better served, or so I thought.

Basically I have a TCustomButton which I have ownerdrawn, this button component has several published properties for changing the appearance such as the border and fill color etc.

The Component Editor I am implementing displays in the context menu a new menu item to "Load settings from a File". When executed a simple TOpenDialog is shown to which you can select the appropriate file, for example an Ini File which I then read and set the values from the File accordingly.

Everything is working good from what I can see, but as I am still sort of new and getting to grips with the whole custom controls side of Delphi I noticed something that does not happen - I am not sure if this is the actual intended behavior or whether I can change it.

The problem is using the Component Editor menu on multiple selected instances of my button control. If just one button is selected and I right click in the Designer, my menu is shown at the top of the context menu, however multiple selected controls do not display the Component Editor menu.

Code Sample

type
  TMyButtonEditor = class(TComponentEditor)
  public
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;

implementation

{ TMyButtonEditor }

procedure TMyButtonEditor.ExecuteVerb(Index: Integer);
var
  OpenDialog: TOpenDialog;
begin
  case Index of
    0:
    begin
      OpenDialog := TOpenDialog.Create(nil);
      try
        OpenDialog.Filter := 'All Files (*.*)|*.*';

        if OpenDialog.Execute then
        begin
          // handle opened file..
        end;
      finally
        OpenDialog.Free;
      end;
    end;
  end;
end;

function TMyButtonEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0:
    begin
      Result := 'Load settings from File...';
    end;
  end;
end;

function TMyButtonEditor.GetVerbCount: Integer;
begin
  Result := 1;
end;

In register procedure unit:

RegisterComponentEditor(TMyButton, TMyButtonEditor);

From what I can see only single components can use a Component Editor at any given time, or am I wrong and they can be used on multiple controls?

I was hoping to select say maybe 3 or 4 of my buttons on the Form Designer and use the Component Editor to apply imported settings on those buttons all at once.


回答1:


Component editors can only operate on a single component.

This is one very good reason to prefer making properties available through the Object Inspector rather than component editors, wherever possible. Because the Object Inspector can operate on multiple components at once.



来源:https://stackoverflow.com/questions/14802371/can-a-component-editor-be-executed-on-multiple-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!