Visual component of a subclass of Delphi and use it

I would like to subclass TToolBar with another MyTToolBar class so that I can override the method. I am new to Delphi, but after testing the various methods for two hours, I cannot use MyTToolBar instead of TToolBar. I cannot be the first person who would like to redefine a method in a class of visual components.

I get more from the Xcode background, where it is easy to subclass the visual component. You create a subclass (e.g. MySubclass) of the parent class (e.g. MySuperClass) and then simply assign the subclass in the Xcode interface builder view. The subclass is automatically recognized and used.

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

After adding a TToolBar to TForm, it is not 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 saying that the toolbar should be of type Vcl.ComCtrls.TToolBar, but declared as MyTToolbar. Correct ad? "It just seems stupid ...

Oh, and I also used the New Component Wizard: File -> New -> Other -> Delphi Projects -> Delphi Files -> Component. I select the ancestor for MyTToolBar as TToolBar and tell him to register on the Swatches panel page. However, it does not appear on the Samples page.

+5
source share
3 answers

XCode "interposer" Delphi. , , IDE TToolBar. , TToolBar, TToolBar, TToolBar. TToolBar , , TForm DFM.

TToolBar TToolBar :

  • TToolBar , TForm:

    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.
    
  • TToolBar , TForm unit uses ComCtrls:

    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.
    

. TToolBar , IDE, , @KenWhite TToolBar. TMyToolBar ( - ), TToolBar , . , " " ( ). TMyToolBar , TForm . , .

+15

, , IDE . , IDE .

- - IDE . . ( .)

File->New->Package (Delphi) IDE. , (, MyComponents.dpk).

File->New->Other->Delphi Files Component . New 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.

, , .

Project Manager ( IDE) Install . IDE. (, , Samples , RegisterComponents.)

( .pas .dfm!). TToolBar, , . .

TToolBar TMyToolBar.

View as Text .

TToolBar TToolBar TMyToolBar.

View as Form . , TMyToolBar . ( ), - ; , Close tab, "" , , , , .

+5

Create a block for the 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 your own existing one) and add a block, select Package.bpl installation on you.

+1
source

All Articles