Alexei Petrov
Alexei Petrov

Reputation: 331

How to initialize TColumnEh

It's a simple question. How to initialize variable Column to make the following statement.

procedure TfmSomeForm.grdSomeGridDblClick(Sender: TObject);
var
  Column: TColumnEh;
  IsSomething: Boolean;
begin
  inherited;   
  //Initialize Column

  IsSomething := False;
  if Column.FieldName = 'SOMETHING' then
    IsSomething := True;

Initialize Column that way

 Column := grdSomeGrid.Columns.FindColumnByName('SOMETHING');

makes no sense and probably will lead to exception or i have to do it here

procedure TfmSomeForm.grdSomeGridCellClick(Column: TColumnEh);
begin
  inherited;
  FIsSomething := False;
  if Column.FieldName = 'SOMETHING' then
    FIsSomething := True;
end;

The problem is that i need this flag onDblClick and i don't want to make it global.

Upvotes: 0

Views: 235

Answers (1)

MartynA
MartynA

Reputation: 30715

You don't say which datatype your grdSomeGrid is. However, with an ordinary TDBGrid, what you seem to want to do is straightforward to do in the DblClick event itself.

procedure TForm1.DBGrid1DblClick(Sender: TObject);
var
  ACol : Integer;
  Pt : TPoint;
  CellValue : String;
begin
  { pick up the mouse cursor pos and convert to DBGrid's internal  coordinates }
  Pt.X := Mouse.CursorPos.X;
  Pt.Y := Mouse.CursorPos.Y;
  Pt := DBGrid1.ScreenToClient(Pt);

 { find the column number of the double-clicked column}
  ACol := DBGrid1.MouseCoord(Pt.X, Pt.Y).X - 1;

  if DBGrid1.Columns[ACol].FieldName = 'SOMETHING' then
    { do what you want}
end;

Update: Victoria helpfully mentioned SelectedIndex, which is another way to get the current column, but I always manage to forget it because its name doesn't include Column and there isn't a direct counterpart for the row (because row operations are based around bookmarks, rather than row indexes).

So, I do it the way I have because it reminds me how to get the active column and row indexes, and it's easy to write a free-standing function which gets both at the same time, like this:

type
  TRowCol = record
    Row,
    Col : Integer;
  end;

function GetRowCol(Grid : TDBGrid) : TRowCol;
var
  Pt : TPoint;
begin
  { pick up the mouse cursor pos and convert to DBGrid's internal  coordinates }
  Pt.X := Mouse.CursorPos.X;
  Pt.Y := Mouse.CursorPos.Y;
  Pt := Grid.ScreenToClient(Pt);

  Result.Row := Grid.MouseCoord(Pt.X, Pt.Y).Y;
  Result.Col := Grid.MouseCoord(Pt.X, Pt.Y).X;

  { adjust Col value to account for whether the grid has a row indicator }
  if dgIndicator in Grid.Options then
    Dec(Result.Col);

end;

Upvotes: 2

Related Questions