RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomDrawGrid.MouseToCell Method

Returns the column and row of the cell at the position with screen coordinates (X,Y).

Pascal
procedure MouseToCell(X: Integer; Y: Integer; var ACol: Longint; var ARow: Longint);
C++
__fastcall MouseToCell(int X, int Y, Longint ACol, Longint ARow);

Call MouseToCell to convert from grid-relative screen coordinates to row and column indexes. The X and Y parameters are the screen coordinates of the point to be converted. MouseToCell returns the ACol parameter as the number of the column over the point (X,Y), and the ARow parameter as the number of the row.  

Usually the MouseToCell method is used in a mouse event handler, which supplies the mouse coordinates as the X and Y parameters of the method call.  

C++ Examples: 

 

/*
This example uses a string grid on a form. When the user
selects a cell in the grid and releases the mouse button,
the column and row coordinates for the cell appear in the
cell. The code for displaying the coordinates is written in
the OnMouseUp event handler.  This example requires a
stringgrid.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  StringGrid1->DefaultColWidth = 100;
}

void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
  int Column, Row;
  StringGrid1->MouseToCell(X, Y, Column, Row);
  StringGrid1->Cells[Column][Row] = "Col " + IntToStr(Column) +
                                    ", Row " + IntToStr(Row);
}

 

Delphi Examples: 

{
This example uses a string grid on a form. When the user
selects a cell in the grid and releases the mouse button,
the column and row coordinates for the cell appear in the
cell. The code for displaying the coordinates is written in
the OnMouseUp event handler.  This example requires a
stringgrid.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.DefaultColWidth := 100;
end;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Column, Row: Longint;
begin
  StringGrid1.MouseToCell(X, Y, Column, Row);
  StringGrid1.Cells[Column, Row] := 'Col ' + IntToStr(Column) +
    ',Row ' + IntToStr(Row);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!