Form Inheritance created in Runtime

1

Follow the code below:

type
  TfObject = class(TForm)
  private
    procedure FormShow(Sender : TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  public
    constructor Create(AOwner : TComponent); override;
    { Public declarations }
  end;

Create code:

constructor TfObject.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);

  Position := TPosition.poScreenCenter;
  WindowState := wsNormal;
  KeyPreview := true;
  AlphaBlend := true;
  AlphaBlendValue := 0;
  BorderStyle := bsSizeable;

  OnShow := FormShow;
  OnKeyDown := FormKeyDown;
  OnClose := FormClose;
end;
The problem is this, according to what I know related to Delphi with OO, I could use this way to be able to inherit the methods contained in the Parent class, but when I do this, I can not use the inherited in the child class, which is in this case the form that is inheriting from TfObject .

I've found that by doing the class this way, I can usually use the form's methods, but when I put it into the form's methods ( OnShow , OnClose , OnKeyDown ) to receive its methods, it behaves as if the child class is doing this.

Now my question ...

How do I get my class TForm to assign its form methods and I just inherit them when I implement a method ( OnShow , OnClose , OnKeyDown )?

Example:

procedure TForm1.FormShow(sender : TObject);
begin
  //faz algo
  inherited;
  //faz algo
end;
    
asked by anonymous 15.02.2017 / 02:10

3 answers

1

You are assigning an event rather than a method directly. I suggest looking at the TCustomForm class that is the parent class of TForm, and there are methods that can be overridden. So, use the DoShow, DoClose, etc. methods. These call events, but can be overridden. See the example:

type
  TForm7 = class(TForm)
  protected
    procedure MetodoSubstituivel; virtual;
    procedure DoShow; override;
  public

  end;

implementation

procedure TForm7.DoShow;
begin
  showmessage('antes');
  inherited;
  showmessage('depois');
end;

procedure TForm7.MetodoSubstituivel;
begin
  //algo aqui
end;
    
16.02.2017 / 03:24
0

To use inherited in a method of the child class, calling the method of the parent class, it should be declared as the virtual directive in the parent class and override in the child class.

    
16.02.2017 / 01:11
0

Ramon, I confess I did not quite understand your need. Do you want the child class event to not happen unless explicitly called?

    
09.03.2017 / 15:32