Form Inheritance created in Runtime

Asked

Viewed 347 times

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 O.O.O., I could use this way to inherit the methods contained in the Father class, but when I do so, I cannot use the inherited in the daughter class, which is in this case the form that is inheriting from TfObject.

I found that by doing the class this way, I can normally use the form methods, but when I put to the form methods (OnShow, OnClose, OnKeyDown) to receive his proper methods, he behaves as if the daughter class were doing this.

Now my question...

How I do for my class TForm assign your 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;

3 answers

1

You are assigning an event and not method directly. I suggest looking at the Tcustomform class that is the parent class of Tform, there are methods that can be overridden. So, use Doshow, Doclose, etc. These call events, but can be replaced. 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;
  • Well, I implemented it earlier this way, but every time I declare the Son Form inherited from the Father, I can use the DoShow Usually, but the FormShow of the child form there is no inheirted, due to when creating the same, it assigns to the method that is running now.

  • Exactly, it turns out that the Formshow is a method declared in the local class (daughter), she is not replacing that of the father. Procedure Formshow(Sender : Tobject); No virtual and no override. This is because it is the method triggered by the event assigned only locally, in this case it has no way to use inherited.

0

Ramon, I confess I don’t quite understand your need. You want the event of the daughter class not to happen unless called explicitly?

0

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

  • I know, the problem is that the virtual method is already stated, but when you create a form by the application, your events have already been successfully created, and you can inherit it quietly, I want to do the same, only declaring only one class, and no need to create the form by the application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.