Alister
Alister

Reputation: 6837

Determine if a double click was in the title of a TDBGrid

I want to know when a record was double clicked on in a TDBGrid, but the OnDblClick event is fired regardless of where in the grid has been clicked.

In Delphi is there a nice clean way of determining if a TDBGrid double click was on the title?

Upvotes: 1

Views: 2274

Answers (2)

Stefano Carfagna
Stefano Carfagna

Reputation: 109

// in the class declaration

type
    THackDBGrid=Class(TDBGrid);   

// function to check if click is on the title

function isClickOnTitle(const dbGrid: TDbGrid; const rowTitleHeight : integer): Boolean;

var
  mousePoint  : TPoint;
  mouseInGrid : TPoint;

begin
  mousePoint  := Mouse.CursorPos;
  mouseInGrid := dbGrid.ScreenToClient(mousePoint);
  result      := mouseInGrid.Y <= rowTitleHeight;
end;

// grid double click event

procedure TForm.dbGridDblClick(Sender: TObject);

var
  rowTitleHeight : integer;

begin
  inherited;

  // trick to get the title row height
  rowTitleHeight := THackDBGrid(gdTestGrid).RowHeights[0];

  if not isClickOnTitle(gdTestGrid, rowTitleHeight) then begin
    bbOk.click;
  end;
end;

Upvotes: 1

Sertac Akyuz
Sertac Akyuz

Reputation: 54802

This is how I do it, it just calculates if the position coincides with the title:

function GridClickIsOnTitle(Grid: TDbGrid): Boolean;
var
  Pt: TPoint;
begin
  Pt := Grid.ScreenToClient(SmallPointToPoint(types.SmallPoint(GetMessagePos)));
  Result := (Grid.MouseCoord(Pt.X, Pt.Y).Y = 0) and (dgTitles in Grid.Options);
end;

I call it from the OnDblClick handler.

Upvotes: 6

Related Questions