Page 1 of 1

Event handling

Posted: Fri Jan 05, 2018 10:26 am
by 18682546
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?

Re: Event handling

Posted: Tue Jan 09, 2018 8:42 am
by yeray
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;