Event handling

TeeGrid VCL / FMX for Embarcadero RAD Studio, Delphi, C++ Builder and Lazarus Free Pascal.
Post Reply
ghanshaw
Newbie
Newbie
Posts: 13
Joined: Wed Dec 13, 2017 12:00 am

Event handling

Post by ghanshaw » Fri Jan 05, 2018 10:26 am

With the OnClick event set on a several TeeGrids in a form I am able to deduce which TeeGrid on the form was clicked but do not see how to get which cell was clicked.
I changed it to OnSelect and can now tell which cell is selected but the Sender comes back as TVCLTeeGrid instead of TTeeGrid and I am unable to determine which TeeGrid was clicked.
I can add the OnClick event back in to get the which TeeGrid was clicked and then get the cell info from the OnSelect event but that seems a bit complicated and has some side effects of the cell not showing selected visually for all coluns.
Is there a better solution? What am I missing?

Yeray
Site Admin
Site Admin
Posts: 9614
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Event handling

Post by Yeray » Tue Jan 09, 2018 8:42 am

Hello,

Using the OnClick event, you could do something like this:

Code: Select all

procedure TTickerForm.TeeGridClick(Sender: TObject);
var Grid: TTeeGrid;
    Col: TColumn;
    ColIndex, RowIndex: Integer;
    CursorPos: TPoint;
begin
  if Sender is TTeeGrid then
  begin
    Grid:=Sender as TTeeGrid;

    if Grid = TeeGrid1 then
       Label2.Caption:='TeeGrid1 clicked'
    else if Grid = TeeGrid2 then
       Label2.Caption:='TeeGrid2 clicked';

    CursorPos:=Grid.ParentToClient(CalcCursorPos);

    Col:=Grid.Columns.FindAt(CursorPos.X, TeeGrid1.Width);
    RowIndex:=Grid.Rows.RowAt(CursorPos.Y, TeeGrid1.Height);

    ColIndex:=-1;
    repeat
      Inc(ColIndex);
    until (ColIndex=Grid.Columns.Count) or (Grid.Columns[ColIndex]=Col);

    if ColIndex<Grid.Columns.Count then
       Label2.Caption:=Label2.Caption + ', Col: ' + IntToStr(ColIndex) + ', Row: ' + IntToStr(RowIndex);
  end;
end;
And with OnSelected event, you could do it as follows:

Code: Select all

procedure TTickerForm.TeeGrid1Select(Sender: TObject);
var ColIndex: Integer;
begin
  Label2.Caption:='';

  if Sender = TeeGrid1.Grid then
  begin
    Label2.Caption:='TeeGrid1 selected';
  end
  else if Sender = TeeGrid2.Grid then
  begin
    Label2.Caption:='TeeGrid2 selected';
  end;

  with (Sender as TVCLTeeGrid).Root do
  begin
    ColIndex:=-1;
    repeat
      Inc(ColIndex);
    until (ColIndex=Columns.Count) or (Columns[ColIndex]=Selected.Column);

    if ColIndex<Columns.Count then
       Label2.Caption:=Label2.Caption + ', Col: ' + IntToStr(ColIndex) + ', Row: ' + IntToStr(Selected.Row);
  end;
end;
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply