Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi subclass visual component and use it

I would like to subclass TToolBar with another class called MyTToolBar so that I can override a method. I'm new to Delphi, but after two hours of trying various methods, I can't get MyTToolBar to be used instead of TToolBar. I can't be the first person who's wanted to override a method on a visual component class.

I come more from an Xcode background where subclassing a visual component is easy. You create a subclass (e.g., MySubclass) of a parent class (e.g., MySuperClass) and then just simply assign the subclass in the Interface Builder view of Xcode. The subclass is automatically recognized and used.

Why can't I seem to do this in Delphi RAD Studio XE3?

After adding a TToolBar to a TForm, it doesn't seem possible to change the class. I tried through the Object Inspector as well as through the .PAS source code file. If I change the class in the .PAS file, I get an error message saying the toolbar "should be of type Vcl.ComCtrls.TToolBar but is declared as MyTToolbar. Correct the declaration?" This just seems silly...

Oh, and I've also used the new component wizard from selecting: File -> New -> Other -> Delphi Projects -> Delphi Files -> Component. I select the ancestor for MyTToolBar as TToolBar and tell it to register in the 'Samples' palette page. However, it doesn't appear in the 'Samples' page.

like image 869
spurgeon Avatar asked Feb 09 '13 00:02

spurgeon


3 Answers

The closest equivilent to your XCode approach is to use an "interposer" class in Delphi. Basically, you do not change the code that the IDE creates for the standard TToolBar usage. You instead declare a new class that derives from the standard TToolBar component but is also named TToolBar and you make it visible to the compiler after the standard TToolBar has been declared. Whichever TToolBar class is last seen by the compiler will be the actual class type that gets instantiated whenever the TForm DFM is streamed.

You can make your custom TToolBar class be seen by the compiler after the standard TToolBar class by one of two different ways:

  1. declare the custom TToolBar class in the same unit as your TForm class:

    unit MyForm;
    
    interface
    
    uses
      ..., Vcl.ComCtrls, ...;
    
    type
      TToolBar = class(Vcl.ComCtrls.TToolBar)
        // override what you need...
      end;
    
      TMyForm = class(TForm)
        ToolBar1: TToolBar; // <-- do not change this!
        ...
      end;
    
    implementation
    
    // implement custom TToolBar as needed...
    
    // normal TForm implementation code as needed ...
    
    end.
    
  2. you can declare the custom TToolBar class in its own unit that is then added to the TForm unit's uses clause after the ComCtrls unit has been added:

    unit MyToolBar;
    
    interface
    
    uses
      ..., Vcl.ComCtrls;
    
    type
      TToolBar = class(Vcl.ComCtrls.TToolBar)
        // override what you need...
      end;
    
    implementation
    
    // implement custom TToolBar as needed...
    
    end.
    

    .

    unit MyForm;
    
    interface
    
    uses
      ..., Vcl.ComCtrls, ..., MyToolBar;
    
    type
      TMyForm = class(TForm)
        ToolBar1: TToolBar; // <- do not change this!
        ...
      end;
    
    implementation
    
    // normal TForm implementation code as needed ...
    
    end.
    

This approach works on a per-project basis only. If you want to use your custom TToolBar class in multiple projects, then you are better off installing it into the IDE, like @KenWhite describes, and use it instead of the standard TToolBar. Go back to naming it TMyToolBar (or whatever), do not name it TToolBar anymore since it is not going to be used as an interposer. Make sure the Package is marked as "Runtime and Designtime" in its Project Options (creating separate runtime-only and designtime-ony Packages is outside the scope of this discussion). TMyToolBar will be available at design-time for you to drop on your TForm like any other component. If it is not, then you did not set it up correctly.

like image 189
Remy Lebeau Avatar answered Nov 16 '22 03:11

Remy Lebeau


To change a component on the existing form, it has to actually be a component that the IDE can create an instance of at design-time. This means the IDE has to be aware of it first, of course.

The way to do this is to create your own descendant component, and actually install it into the IDE in a design-time package. You can then drop it on your form instead of the standard version, or replace it on existing forms with a little work. (You do have to create your version and install it first, though.)

Start with File->New->Package (Delphi) from the IDE's menu. Save the package as you would any other project (for instance, MyComponents.dpk).

Now use File->New->Other->Delphi Files, and double-click Component in the right pane. The New Component wizard will start, where you can choose the existing component you want to descend from (or design a new one).

Follow the steps of the wizard, and you'll end up with the basic shell of your component:

unit MyToolBar1;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.ToolWin, Vcl.ComCtrls;

type
  TMyToolBar = class(TToolBar)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TMyToolBar]);
end;

end.

Implement whatever functionality you want in the new descendant, and then save the file.

Right-click on the package in the Project Manager (by default the upper right window in the IDE), and choose Install from the context menu. This will compile and build the package, and automatically install it in the IDE. (The example I've shown would put the new component on the Samples page in the palette based on what's indicated in the RegisterComponents call.)

After doing the above, you can change an existing form (make a backup of the .pas and .dfm files first!). I'll use the TToolBar you mentioned, and the sample replacement I've posted the shell for in the instructions below.

Manually change the classname in the source code editor from TToolBar to TMyToolBar.

Right-click on the form, and choose View as Text from the context menu.

Find the TToolBar, and change it from TToolBar to TMyToolBar.

Right-click again, and choose View as Form from the context menu. If you've done these steps correctly, clicking on the toolbar should show you TMyToolBar in the Object Inspector. If you don't see it (or if you get error messages) you've done something wrong; you can close the tab by right-clicking it at the top of the Code Editor and choosing Close tab, and answer "No" to the prompt about saving changes, and then if necessary restore from the backup copies I told you to make first.

like image 40
Ken White Avatar answered Nov 16 '22 02:11

Ken White


Create a unit for you class:

Unit YourComponent;
interface
uses
 ....
Type
 TYourNewClass=Class(ExistingClass)
   private
   ...
   protected
   ...   
   public
   ...
   published
  end;

  procedure Register;

implementation

.....

procedure Register;
begin
  RegisterComponents('YourPalette', [TYourNewClass]);
end;

create a new package (or open an own existing) and add you unit choose install on you Package.bpl.

like image 26
bummi Avatar answered Nov 16 '22 02:11

bummi