Recent

Author Topic: [Solved] Edit data verification ...  (Read 540 times)

Z80

  • New Member
  • *
  • Posts: 13
[Solved] Edit data verification ...
« on: March 20, 2022, 08:43:43 pm »
Hello.

I am building an application where I have the user edit a few cells which are initialized with some data.
I would like to be able to make a little QA on the data entered before committing the changes of the cell content. So I consider committing the changes if:

  • The data is valid
  • The user did not hit [Esc]

It is my impression that that could be called by the TControl.EditingDone event (or something else) ...

Are there any good examples of such a procedure?
« Last Edit: March 22, 2022, 10:54:14 pm by Z80 »

wp

  • Hero Member
  • *****
  • Posts: 10030
Re: Edit data verification ...
« Reply #1 on: March 20, 2022, 09:45:19 pm »
I suppose you mean the WorksheetGrid? Normally in a StringGrid, you can validate user input in the OnValidate event. But this event is fired every time when a key is pressed. In case of a spreadsheet, this is too often because this would produce a lot of error messages while typing a formula. It is better to check input when editing is done, i.e. when the user presses the ENTER key or selects another cell. And this is the moment, when the OnEditingDone event  is fired.

If you want to restore the previous cell content in case of an input error you must save the previous cell content in the OnGetEditText event since the new text already has been committed to the spreadsheet when OnEditingDone fires.

The following code checks whether the entered text is a number, but reverts it to the previous cell content if it is not between 0 and 100.

Code: Pascal  [Select][+][-]
  1. var
  2.   FOldCell: String;
  3.  
  4. procedure TForm1.sWorksheetGrid1EditingDone(Sender: TObject);
  5. var
  6.   s: String;
  7.   x: double;
  8. begin
  9.   s := sWorksheetGrid1.Cells[sWorksheetGrid1.Col, sWorksheetGrid1.Row];
  10.   if TryStrToFloat(s, x) and ((x < 0) or (x > 100)) then
  11.     sWorksheetGrid1.Cells[sWorksheetGrid1.Col, sWorksheetGrid1.Row] := FOldCell;
  12. end;
  13.  
  14. procedure TForm1.sWorksheetGrid1GetEditText(Sender: TObject; ACol,
  15.   ARow: Integer; var Value: string);
  16. begin
  17.   FOldCell := Value;
  18. end;

Z80

  • New Member
  • *
  • Posts: 13
Re: Edit data verification ...
« Reply #2 on: March 21, 2022, 07:31:15 am »
I suppose you mean the WorksheetGrid ...
Yes, manipulations are done in WorksheetGrid. Thank you very much for the very fulfilling answer.

 

TinyPortal © 2005-2018