cell is validated twice with message box

A cell is validated twice, if

  1. cell value is changed and editing is finished with enter key
  2. CellValidate shows message box and returns with Valid parameter set to false.


To reproduce, put a AdvStringGrid on a form, assign FormCreate to then form's OnCreate Event, assign AdvStringGrid1CanEditCell to the grid's OnCanEditCell Event, assign AdvStringGrid1CellValidate to the grid's OnCellValidate event and add the following code. Then, change the cell with value 200 (i.e. to value 220) and press the enter key. The message box will be shown twice.


procedure TForm1.AdvStringGrid1CanEditCell(Sender: TObject; ARow, ACol: Integer;
  var CanEdit: Boolean);
begin
  CanEdit := ACol = 0;
end;

procedure TForm1.AdvStringGrid1CellValidate(Sender: TObject; ACol,
  ARow: Integer; var Value: string; var Valid: Boolean);
begin
  if ACol = 0 then
  begin
    MessageDlg('invalid value', mtWarning, [mbOk], 0);
    Valid := False;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AdvStringGrid1.FixedCols := 0;
  AdvStringGrid1.OnCanEditCell  := AdvStringGrid1CanEditCell;
  AdvStringGrid1.OnCellValidate := AdvStringGrid1CellValidate;
  AdvStringGrid1.Cells[0, 1] := '200';
end;


There should not be code that shows a message dialog in the OnCellValidate event. A message dialog takes the focus away from the grid and the focus leaving the grid interferes with the process of stopping the inplace editor. If you need to show a message dialog, this must be done from the OnEditCellDone event.