🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.vcl.components.using.general
4 messages
4 total messages Started by Paul Fri, 18 Jul 2008 04:48
How to obtain the coordinates of the currently selected (active) row in DBGrid
#165
Author: Paul
Date: Fri, 18 Jul 2008 04:48
70 lines
2500 bytes
Hi -

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
Re: How to obtain the coordinates of the currently selected (active) row in DBGrid
#169
Author: Remy Lebeau (TeamB)
Date: Fri, 18 Jul 2008 17:39
35 lines
1989 bytes
"Paul" <nospam@sdiespammersdie.com> wrote in message 
news:df70849ccscho5b5svki9i5tdc8flgqu3b@4ax.com...

> 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 
Re: How to obtain the coordinates of the currently selected (active) row in DBGrid
#171
Author: Chris Luck
Date: Sat, 19 Jul 2008 01:00
46 lines
2103 bytes
> 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.


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
Re: How to obtain the coordinates of the currently selected (active) row in DBGrid
#181
Author: Paul
Date: Tue, 22 Jul 2008 10:43
51 lines
2463 bytes
Many thanks Gambit - 

I will try your suggestion,,

Cheers..

On Fri, 18 Jul 2008 10:39:29 -0700, "Remy Lebeau \(TeamB\)"
<no.spam@no.spam.com> wrote:

>
>"Paul" <nospam@sdiespammersdie.com> wrote in message 
>news:df70849ccscho5b5svki9i5tdc8flgqu3b@4ax.com...
>
>> 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 
>

OS : Windows XP Service Pack 2
Delphi : 2005 Service Pack 3 Installed
Version 9.0.1935.22056
CPU : AMD 3500+
Memory 1Gig
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads