[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PopupMenuEditor
Welcome to Component Writer's Help - Editors

Adding a PopupMenu to your Component !


Hint: When writing your editors, write them in a separate file (example MyEditors.pas). That way, you will not need to include them in your applications (uses clause) each time you want to use your custom components.


Let's say you have a component called MyComponent which has a property MyCollection of type TOwnedCollection. You would like to be able to save your items to a file in order for your fellow developpers to be able to load the same items (configuration) when they start using your custom component !

But, you don't want to create an application and use it to call your MyComponent.Collection.SaveToFile method !!!

You want to create that file directly from your Delphi Design interface.

Well, you need to create Component Editor !.




First, create a new custom component editor that comes from the inherited class TComponentEditor (unit DesignEditors, DesignIntf). Then you need to override theses 3 methods :

procedure ExecuteVerb(Index: Integer)

This procedure will call your custom Verb (Action) depending on the index. Don't worry, the PopupMenu will correctly call this method !

function GetVerb(Index: Integer): string;

This procedure return the verb name. This is the string that will be visible in your Component's Popup menu, so be carefull what you write !

procedure GetVerbCount: Integer

This procedure will return the number of Verbs, actions. In other words, the number of items in your Popup menu.

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





Now, this is where the FUN starts !

In the ExecuteVerb procedure, you need to put the code that will be executed by your different actions.

In this example, Verb 1 (index = 0), we show the Delphi Collection Editor. This editor can be called by using the ShowCollectionEditor function, which can be found in the Unit ColnEdit.

The second verb shows a OpenDialog in order to load the items. The last verb shows a SaveDialog to save the items.

procedure TMyComponentEditor.ExecuteVerb(Index: Integer);
var
  loOpenDialog: TOpenDialog;
  loSaveDialog: TSaveDialog;
begin
  inherited;
  case Index of
    0: begin
        // Edit my porperty MyCollection of type TOwnedCollection
        // You need unit ColnEdit
        ShowCollectionEditor(Designer, Component, TMyComponent(Component).MyCollection, 'Variables');
       end;
    1: begin
        // Open File
        loOpenDialog := TOpenDialog.Create(nil);
        try
          with loOpenDialog do begin
            Title      := 'Load Items';
            DefaultExt := '*.var';
            Filter     := 'Items files (*.var)|*.VAR';
            if Execute
            then begin
              TMyComponent(Self.Component).MyCollection.LoadFromFile(FileName);
              Self.Designer.Modified;
            end;
          end;
        finally
          loOpenDialog.Free;
        end;
       end;
    2: begin
        // Save File
        loSaveDialog := TSaveDialog.Create(nil);
        try
          with loSaveDialog do begin
            Title      := 'Save Items';
            DefaultExt := '*.var';
            Filter     := 'Items files (*.var)|*.VAR';
            if Execute
            then begin
              TMyComponent(Self.Component).MyCollection.SaveToFile(FileName);
              Self.Designer.Modified; // let the designer know you have changed
            end;
          end;
        finally
          loSaveDialog.Free;
        end;
       end;
  end;
end;


In the GetVerb function, you need to put the code that will return the names of the different Verbs. Theses names will appear in your Component's popup menu at Design time.

function TMyComponentEditor.GetVerb(Index: Integer): string;
begin
  Result := '';
  case Index of
    0: Result := 'Edit Items';
    1: Result := 'Load Items';
    2: Result := 'Save Items';
  end;
end;


Finally, in the GetVerbCount function, you need to return the number of Verbs.

function TMyComponentEditor.GetVerbCount: Integer;
begin
  Result := 3;
end;





Registering your Component Editor

Your Finished !! Now you need to Register your Component Editor

...
procedure Register
implementation
procedure Register;
begin
  RegisterComponents('Standard', [TMyComponent]);
  RegisterComponentEditor(TMyComponent, TMyComponentEditor);
end;
...




last edited (March 28, 2005) by Tief, Number of views: 4141, Current Rev: 8 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.