Question

I am doing a custom component, descendent of TComponent in Delphi XE2 and I want to right click on it and have 2 option: Next and Previous (for example)

How can that be done? Please supply a code snippet or example.

That option should appear at design time, right clicking on the component dropped on the form, right now is a descendant of TComponent. But that is not required if there is a better option to have the functionality.

This next and previous will be used to switch colors of components (via owner component list) based on a themes in a collection.

Was it helpful?

Solution

You need to register a custom component editor for your component, and override three methods:

function GetVerbCount: Integer;
function GetVerb(Index: Integer): string; 
procedure ExecuteVerb(Index: Integer);

Here's an (extremely minimal) example, thrown together quickly in Delphi 2007:

MyTestComponentPackage.dpk - A new VCL package (File->New->Package)

package MyTestComponentPackage;

{$R *.res}
{$ALIGN 8}
{$ASSERTIONS ON}
{$BOOLEVAL OFF}
{$DEBUGINFO ON}
{$EXTENDEDSYNTAX ON}
{$IMPORTEDDATA ON}
{$IOCHECKS ON}
{$LOCALSYMBOLS ON}
{$LONGSTRINGS ON}
{$OPENSTRINGS ON}
{$OPTIMIZATION ON}
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
{$REFERENCEINFO ON}
{$SAFEDIVIDE OFF}
{$STACKFRAMES OFF}
{$TYPEDADDRESS OFF}
{$VARSTRINGCHECKS ON}
{$WRITEABLECONST OFF}
{$MINENUMSIZE 1}
{$IMAGEBASE $400000}
{$IMPLICITBUILD ON}

requires
  rtl,
  DesignIDE;

contains
  MyComponentUnit in 'MyComponentUnit.pas',
  MyCompRegUnit in 'MyCompRegUnit.pas';
end.

MyComponentUnit.pas - A new Delphi unit for the component code itself (File->New->Unit). Unit declares a custom type for the back/forward functionality. The component does absolutely nothing but declare a property of that type we can set via the popup menu at designtime.

unit MyComponentUnit;

interface

uses
  Classes;

type
  TMyComponentDirection = (cdBack, cdForward);

type
  TMyComponent=class(TComponent)
  private
    FDirection: TMyComponentDirection;
  published
    property Direction: TMyComponentDirection read FDirection write FDirection;
  end;


implementation

end.

MyCompRegUnit.pas, which implements the custom component editor and registers both the component and its editor:

unit MyCompRegUnit;

interface

uses
  DesignIntf, DesignEditors, Classes;

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

  procedure Register;

implementation

{ TMyComponentEditor }

uses
  MyComponentUnit;

// Called when component of this type is right-clicked. It's where
// you actually perform the action. The component editor is passed a reference
// to the component as "Component", which you need to cast to your specific
// component type
procedure TMyComponentEditor.ExecuteVerb(Index: Integer);
begin
  inherited;
  case Index of
    0: (Component as TMyComponent).Direction := cdBack;
    1: (Component as TMyComponent).Direction := cdForward;
  end;
end;

// Called the number of times you've stated you need in GetVerbCount.
// This is where you add your pop-up menu items
function TMyComponentEditor.GetVerb(Index: Integer): string;
begin
  case Index of
    0: Result := '&Back';
    1: Result := '&Forward';
  end;
end;

// Called when the IDE needs to populate the menu. Return the number
// of items you intend to add to the menu.
function TMyComponentEditor.GetVerbCount: Integer;
begin
  Result := 2;
end;

procedure Register;
begin
  RegisterComponents('TestStuff', [TMyComponent]);
  RegisterComponentEditor(TMyComponent, TMyComponentEditor);
end;

end.

You'll need to save and build the package, and then right-click it in the Project Manager and choose "Install". It will register the component on the TestStuff component palette page.

Save all, and then start a new VCL forms application. In the Component Palette, type TMy to locate the new component, and double-click it to add it to the new form. Right-click it on the form:

Popup menu at designtime with back and forward items added

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top