K
K
KTG2017-01-22 09:26:22
Delphi
KTG, 2017-01-22 09:26:22

How to add additional properties to objects of the same class and make the handler one for all?

There is a DBGrid on the form.
It has an OnDataHintShow event.
I described it. But I realized that this code will be repeated for many Grids, so I decided to put it in a separate procedure and pass parameters there.
When I described a couple more handlers, I realized that the parameters remain the same as in the previous one, but again, the code will be repeated and you need to take it out.
Therefore, I created a class:
TDynamicGrid = class(DBGridEh)
Described its properties and procedure, for example ShowPanel (which pops up on OnDataHintShow).
There were questions:
1. How to make, that Grid (DBGridEh) located on the form, became TDynamicGrid.
It is configured, fitted, designed, and I would not want to create it programmatically and prescribe all the parameters.
If in the description of the form, I change the class to the required one in the code of this grid - Delphik swears, says that it is incorrectly defined, but the error can be ignored. Then everything works, but! The error "Exception EInvalidPointer"
2 pops up. All the same, you have to enter the grid and register a call to this procedure in the OnDataHintShow event.
Is it possible to somehow write an event handler in a class so that it would act by default for all objects of this class?

unit DynamicGrid;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.UITypes, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, CompoMansEh, Vcl.ExtCtrls,
  PivotGridToolsEh, Data.DB, EhLibVCL, GridsEh, DBAxisGridsEh, DBVertGridsEh,
  Vcl.StdCtrls, Vcl.DBCtrls, Data.Win.ADODB, DBGridEhGrouping, ToolCtrlsEh,
  DBGridEhToolCtrls, DynVarsEh, DBGridEh, System.ImageList, Vcl.ImgList;


type
  TDynamicGrid = class(TDBGridEh)
    private
      PanelOnGrid: TPanel;
      HighlightColor: TColor;
      GlobalArrSList: TStringList;
    public
      property GridPanel : TPanel read PanelOnGrid write PanelOnGrid;
      property HColor: TColor read HighlightColor write HighlightColor;
      property GlobalArr: TStringList read GlobalArrSList write GlobalArrSList;

      procedure ShowPanel(Cell: TGridCoord);
      constructor Create(AOwner : TComponent); override;
      destructor Destroy; override;
 end;

implementation

procedure TDynamicGrid.ShowPanel(Cell: TGridCoord);
var
 rect: TRect;
 rectX, rectY : extended;
 Name, ID: string;
begin
  rect := self.CellRect(Cell.X, Cell.Y);

    rectX := Rect.TopLeft.X + Rect.Width;
    rectY := Rect.TopLeft.Y + Rect.Height/2 + Rect.Height - 1;

   if Cell.Y = self.Row then
    self.PanelOnGrid.Color := self.HColor
   else
    self.PanelOnGrid.Color := clWhite;

    self.PanelOnGrid.Left := trunc(rectX - self.PanelOnGrid.Width/2);
    self.PanelOnGrid.Top := trunc(rectY);

    self.PanelOnGrid.Visible := true;

    Name := self.DataSource.DataSet.FieldByName('Name').AsString;
      ID := self.DataSource.DataSet.FieldByName('ID').AsString;

    self.GlobalArr.Values['ID'] := ID;
    self.GlobalArr.Values['Name'] := Name;

    TForm(self.Parent).caption := 'Справочник: ' + Name;
end;

constructor TDynamicGrid.Create(AOwner: TComponent);
begin
  inherited;
end;

destructor TDynamicGrid.Destroy;
begin
  inherited;
end;

end.

So. DBGrid has a column, and it has an OnDataHintShow event.
Delphi handler is called like this: TForm.GridNameColumns0DataHintShow.
where 0 is apparently the column number, GridName is the name of the Grid.
I want this event to automatically call the ShowPanel procedure. No matter what you have to write by hand.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kalapanga, 2017-01-22
@kalapanga

DBGridEh and everything about it is also written in dfm. You can try editing it by hand. It might roll.
Well, for good, you need to complete the TDynamicGrid as a component in order to work with it in the usual way in design time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question