2006. október 30., hétfő

How to detect a double-click in a column of a TDBGrid


Problem/Question/Abstract:

When a user double-clicks on a DBGrid, how can I tell which column they were double-clicking on? Or does that double-click only apply to an entire row? I'd like to set that field to toggle values everytime the user double-clicks.

Answer:

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
  pt: TPoint;
  gc: TGridCoord;
begin
  GetCursorPos(pt);
  with Sender as TDBGrid do
  begin
    pt := ScreenToClient(pt);
    gc := MouseCoord(pt.x, pt.y);
    if Columns[gc.X - 1].Color = clRed then
      Columns[gc.X - 1].Color := clBlue
    else
      Columns[gc.X - 1].Color := clRed;
  end;
end;

No need for the "hack" class technique here, though that also works.

Nincsenek megjegyzések:

Megjegyzés küldése