How to add event to an inherited form event in Delphi?

Asked

Viewed 438 times

0

I explain, I have Fmodelo1 that has several standard functions, among them I have the creation of the Oncellclick event of a Dbgrid that is inherited to all the Formularios created from Fmodelo1, works beauty, now I need for example that the Fclientes that is inherited from Fmodelo1 has beyond the Oncellclick event heritage of Fmodelo1 its own event Cellclick added when clicked.

This is not happening because the event is being replaced by the Fmodelo1 event

I’ll try to put part of the code:

In the Onshow of Fmodelo1 I do several checks one of them is if exite Dbgrid in the Form, finding a Dbgrid I replace some events among them the Cellclick as abyss:

...
TDBGrid(Components[i]).OnCellClick := DBGridCellClick;
...

Dbgridcellclick is a Procedure with a standard routine for all Forms:

procedure TFmodelo1.DBGridCellClick(Column: TColumn);
begin
  if Column.PickList.Count > 0 then
  begin
    keybd_event(VK_F2, 0, 0, 0);
    keybd_event(VK_F2, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, 0, 0, 0);
    keybd_event(VK_DOWN, 0, 0, 0);
    keybd_event(VK_DOWN, 0, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
  end;
end;

There in Fclientes has its own Cellclick with its particular routine, I can cancel in the Onshow the Fmodelo1 process with the line:

JvDBUltimGridClientes.OnCellClick := JvDBUltimGridClientesCellClick;

But I kind of wanted it to be something like this:

DBGridClientes.OnCellClick := DBGridCellClick + DBGridClientesCellClick;

In other words, he would take the events of Fmodelo1 + Fclientes

  • Post your code for a more complete answer.

  • I’m going to try to put part of it, because it’s kind of complex

2 answers

3


In that case you will have to override the class event TFModelo1 in TFClientes, would look something like this:

type
  TFClientes = class(TFModelo1)
  private
    FOnCellClick: TDBGridClickEvent;
    procedure SetOnCellClick(const Value: TDBGridClickEvent);

    procedure OnCellClick(Column: TColumn);
  public
    property OnCellClick: TDBGridClickEvent read FOnCellClick write SetOnCellClick; // Propriedade onde estará a procedure a ser executada

    constructor Create; reintroduce;
  end;

implementation

{ TFClientes }

constructor TFClientes.Create;
begin
  //Ao construir, você já passa a procedure desta classe pra ser executada
  inherited OnCellClick := FOnCellClick;
end;

procedure TFClientes.OnCellClick(Column: TColumn);
begin
  //Se foi passada alguma procedure para o OnCellClick, execute
  if (Assigned(FOnCellClick)) then
    FOnCellClick(Column);
end;

procedure TFClientes.SetOnCellClick(const Value: TDBGridClickEvent);
begin
  FOnCellClick := Value;
end;
  • +1 for the correct answer and have used the reintroduce in the constructoor

  • not everyone uses, what generates Warning and ends up working improperly!

  • @Roberto de Campos, I understand, now there is how to make the Model add the events in the inherited forms? Imagine that I have N forms created with Fmodelo1, I would have to enter one by one to add this routine, if I can treat it there in Fmodelo1 he could add the inherited events in all their dependents, I don’t know if I was clear.

  • I’ve never done it, but I believe that if you make one array of procedures, you can sweep and run. I don’t know if it’s the best way out of this.

  • Or you can also create a OnCellClickExtra for example.

  • Your answer already gave me another solution so I don’t have to set the procedures in the onshow of the Formulars when they already exist: if not (Assigned(Tjvdbultimgrid(Components[i]).Oncellclick)) then Tjvdbultimgrid(Components[i]). Oncellclick := Jvdbultimgridcellclick;

Show 1 more comment

1

You can call Handler the ancestor event:

procedure TFClientes.OnCellClick(Column: TColumn); begin inherited; //executa OnCellClick do FModelo1 ... //seu código do FClientes aqui end;

  • Thanks for the reply Ricardo, but it turns out that the event is created dynamically, there is until then Dbgrid la in Fmodelo1 so the event is not inherited but replaced.

Browser other questions tagged

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