Q:

Finding out position of a control inside TGridPanel

How I can find out the position (row and column index) of controls inside TGridPanel? I'd like to use common OnClick event for number of buttons and need to know the X,Y position of the button.
I'm using Delphi 2007.

A:

Unfortunately, because of the magic of TGridPanel, it is a little more complicated than just getting the Top and Left properties...  
This should do it for any Control, adapt it to your needs:
procedure GetRowColumn(const AControl: TControl; var ARow, AColumn: Integer);
var
  I: Integer;
begin
  if AControl.Parent is TGridPanel then
  begin
    I := TGridPanel(AControl.Parent).ControlCollection.IndexOf(AControl);
    if I > -1 then
    begin
      ARow := TGridPanel(AControl.Parent).ControlCollection[I].Row;
      AColumn := TGridPanel(AControl.Parent).ControlCollection[I].Column;
    end;
  end;
end;

procedure TForm1.ButtonClick(Sender: TObject);
var
  Row, Column : Integer;
begin
  GetRowColumn(Sender as TControl, Row, Column);
  // do something with Row and Column
  ShowMessage( Format('row=%d - col=%d',[Row, Column]));
end;

