Can anyone tell me how to obtain the coordinates for the currently
selected row ini a DBGrid ?
Now I need to obtain the screen coordinates for pop-up a menu when the
user presses a key at the position of the currently highlighted row.
As I am using a key press - I cannot rely on a mouse-click event to
trap the coordinates.
I have seen 2 suggestions on the Internet so far, and neither seem to
work.
The first suggests that I user the "OnDrawColumnCell" even to
initialise somevariables - and then use these as the coordinates to
pop-up the menu or whatever. Tis is the meathod I am using at the
moment..
Somerthing like..
OnDrawColumnCell
================
if gdFocused in State then
begin
CurrentLeft := Rect.Left;
CurrentTop := Rect.Top;
}
================
Then use..
=======================
Var
tmpPOint : TPoint;
TmpPoint.X := CurrentLeft;
TmpPoint.y := CurrentTop;
=======================
Unfortunately - this will not work.
The other meathod suggests I use something like..
========================================
var
pnt : TPoint;
begin
pnt :=
Form1.ScreenToClient(StringGrid1.ClientToScreen(StringGrid1.CellRect(0,
2).TopLeft));
caption := IntToStr(pnt.Y);
end;
=========================================
But DBGrid does not have (or expose) the CellRect property.
Any help would be very much appreciated..
Cheers..
Dave.
OS : Windows XP Service Pack 2
Delphi : 2005 Service Pack 3 Installed
Version 9.0.1935.22056
CPU : AMD 3500+
Memory 1Gig
> Can anyone tell me how to obtain the coordinates for the
> currently selected row ini a DBGrid ?
TDBGrid inherits a CellRect() method from TCustomGrid.
> But DBGrid does not have (or expose) the CellRect property.
It is not a property. It is a method. It is declared as 'protected' in
TCustomGrid, and TDBGrid does not promote it to 'public'. You can use an
accessor class to gain access to CellRect(), ie:
type
TDBGridAccess = class(TDBGrid)
public
function CellRect(ACol, ARow: Integer): TRect;
end;
function TDBGridAccess.CellRect(ACol, ARow: Integer): TRect;
begin
Result := inherited CellRect(ACol, ARow);
end;
var
pnt : TPoint;
begin
pnt := TDBGridAccess(DBGrid1).CellRect(0, DBGrid1.Row);
// use pnt as needed...
end;
Gambit
> Now I need to obtain the screen coordinates for pop-up a menu when the
> user presses a key at the position of the currently highlighted row.
This gives you the cell location, if you only want the row remove the
SelectedIndex reference. FixedCols accounts for the possible presence of the
Indicator column, no adjustment is needed for a Title row. Here I've used
the Context-Menu key for activation.
--------------------
unit Unit1;
interface
uses
Windows, ...
// Access protected members via a sub-class
type
TAccessDBGrid = class(TDBGrid);
type
TForm1 = class(TForm)
...
procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
Pt: TPoint;
begin
if Key = VK_APPS then
begin
with TAccessDBGrid(DBGrid1) do
Pt := ClientToScreen(CellRect(SelectedIndex+FixedCols,Row).TopLeft);
PopupMenu1.Popup(Pt.X + 5, Pt.Y + 5);
end;
end;
--
Regards,
Chris Luck
I will try your suggestion,,
Cheers..
OS : Windows XP Service Pack 2