[Lazarus] Stringgrid woes...

Michael Van Canneyt michael at freepascal.org
Fri Mar 13 20:40:27 CET 2009



On Fri, 13 Mar 2009, Jesus Reyes wrote:

> > This makes it completely impossible to do any checking of
> > the entered
> > values, because the SetEditText() event has no way of
> > knowing when the
> > editing ends. In my opinion, the SetEditText() should only
> > be called
> > when the editing is done, i.e. it should be like this:
> > 
> > procedure TStringCellEditor.Change;
> > begin
> >   inherited Change;
> > end;
> > 
> > procedure TStringCellEditor.EditingDone;
> > begin
> >   inherited EditingDone;
> >   if FGrid<>nil then
> >     begin
> >     FGrid.SetEditText(FCol, FRow, Text);
> >     FGrid.EditingDone;
> >     end;
> > end;
> 
> This suggest that there should not be distinction between grid's OnSetEditText and OnEditingDone so why do we need both?.
> 
> > 
> > Imagine a stringgrid where you want to allow only real
> > numbers in a
> > cell. In that case you normally do 2 things:
> > - Only allow numerals and  decimal points in the OnKeyPress
> > event
> > - Only save the edit text when a valid number is entered,
> > and give 
> >   an error message if it is not valid.
> > 
> > With the current situation, this is simply impossible,
> > because of the
> > following:
> > 1. User types 1 -> Settext receives 1 -> all is well.
> > 2. User types . -> settext received 1. -> error,
> > because 1. is not a valid number.
> > 
> > In other words, there is no way of knowing when you have
> > received the final
> > text that the user has entered. 
> > 
> > Is there a reason for the current behaviour, and can it be
> > changed ?
> 
> The grid behaves that way for the same reason there is a TEdit.OnChange event, 
> I'm afraid there is no way to know that final text has been entered using this event.
> 
> Is this different on Delphi? I would like to improve compatibility if it's not. 
> If OnSetEditText is enough in Delphi to know when editing is finished then I think we should do it also.

Hm. 
Did some checking in delphi. It behaves the same. So better leave it as it is.
Bad :(

Oh well, now I know again why I don't want to use grids for editing. 
But customers disagree, unfortunately :(

> Currently the assumed way to know when the final text is entered is using OnEditingDone no OnSetEditText, 
> maybe this is not working but I was aware that OnEditingDone was triggered for several causes not necesarily 
> only when user ended editing, for this reason I usually do following on OnEditingDone of grid cells that need 
> validation.
> 
>   if grid.Modified then begin
>     [Do validation on col,row cell. Revert changes if not validated, etc]
>     grid.Modified := false;
>   end;

The problem is that the actual data is not in the grid cells, so reverting it would not be that easy.
Is there a way to force the grid to save the data ? That would help, actually...

In Delphi, Setting 

  EditorMode:=False;

Saves the data (setedittext is called again). In Lazarus, it is not called. 

I think it would also be a good idea to have an event which helps to validate the data.

Something like

Procedure TForm1.ValidateEntry(Sender : TObject; ACol,ARow : Integer; Var Value : String);

begin
  // Do some things, possibly modify Value or raise exception.
end;

Which is called only once, when the data is saved finally. 
Currently, it's hard to do proper validation in a grid. Well, in my opinion of course...

Michael.



More information about the Lazarus mailing list