Recent

Author Topic: Checking Date Format in StringGrid  (Read 1255 times)

Badger

  • Full Member
  • ***
  • Posts: 144
Checking Date Format in StringGrid
« on: April 14, 2020, 03:06:36 am »
Hi All

I'm trying to trap incorrect date input in a string grid by checking the previous cell when OnSelectCell is called.

Getting it to trap the error is easy but I want it to return focus to the cell with the error.  The following code traps the error but the new cell still gets focus.

Any (helpful) suggestions?

Code: Pascal  [Select][+][-]
  1. procedure TIncomeExpenditureForm.IncomeSelectCell(Sender: TObject; aCol,
  2.   aRow: Integer; var CanSelect: Boolean);
  3. var
  4.   dummydate:TDate;
  5. begin
  6.   //Check for proper Date format
  7.   if (PreviousCell.x=0) and (Income.Cells[PreviousCell.x,PreviousCell.y] <>'') then
  8.   begin
  9.     try
  10.       dummydate:=StrToDate(Income.Cells[PreviousCell.x,PreviousCell.y]);
  11.     Except
  12.       ShowMessage('Date Format is incorrect');
  13.       //move focus back to previous cell
  14.       Income.Col:=PreviousCell.x;
  15.       Income.Row:=PreviousCell.y;
  16.     end;
  17.   end;
  18.  
  19.   //Remember New Cell Position
  20.   PreviousCell.x:=aCol;
  21.   PreviousCell.y:=aRow;
  22.  
  23. end;        
             
« Last Edit: April 14, 2020, 09:12:36 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

jamie

  • Hero Member
  • *****
  • Posts: 6133
Re: Checking Date Format in StringGrid
« Reply #1 on: April 14, 2020, 04:02:23 am »
are you sure the Exception is getting raised ?

 So you see the MessageBox ?

Also it could be that the cell is repositioned after the call block..

You may need to take it out of edit mode move it and then put it back into edit mode..

if that does not work then just send a User message to the form via a PostMessage and then process it later when the code has left this block...

  But I would try to take it out of edit mode first before moving it then put it back into edit mode.
The only true wisdom is knowing you know nothing

bytebites

  • Hero Member
  • *****
  • Posts: 640
Re: Checking Date Format in StringGrid
« Reply #2 on: April 14, 2020, 07:47:23 am »
Code: Pascal  [Select][+][-]
  1. procedure TIncomeExpenditureForm.IncomeValidateEntry(sender: TObject; aCol, aRow: Integer;
  2.   const OldValue: string; var NewValue: String);
  3. var
  4.   dummydate: TDateTime;
  5. begin
  6.   if (aRow=0) and not TryStrToDate(Income.Cells[aCol,aRow],dummydate) then NewValue:=OldValue
  7. end;
  8.  

Badger

  • Full Member
  • ***
  • Posts: 144
Re: Checking Date Format in StringGrid
« Reply #3 on: April 14, 2020, 09:09:44 am »
Quote
are you sure the Exception is getting raised ?

 So you see the MessageBox ?
Yes  the message box appears.  It's just that for some reason (or perhaps a very good reason) the program seems to ignore the setting of Row and Col and just moves focus to the cell originally clicked in stead of focusing on the previous cell.

Quote
procedure TIncomeExpenditureForm.IncomeValidateEntry(sender: TObject; aCol, aRow: Integer;
  const OldValue: string; var NewValue: String);
I don't  think that will help, I want the program to return to the previous cell so the user can correct it.

jamie's other suggestions are beyond my limited abilities

« Last Edit: April 14, 2020, 09:43:17 am by Badger »
Badger
(A bad tempered, grumpy animal that sleeps most of the winter!)

If at first you don't succeed - you're running about average!

I'm using Windows 10 Lazarus v2.4.4  FPC 3.2.2   Win 32/64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Checking Date Format in StringGrid
« Reply #4 on: April 14, 2020, 10:13:07 am »
You either need to use bytebytes' solution, or set a cell editor that is a date editor (rather than a string editor) such as TDateEdit, which would prevent user entry of an invalid dates.

Trying to roll your own validation routine and ignoring the built in OnValidateEntry event will almost certainly lead to grief.
OnSelectCell is a poor choice, since it is called so frequently, and in a large grid code such as you show, even if it worked, would slow the UI.
TStringGrid has been designed with an underlying, largely hidden, internal messaging system. Without understanding that you are unlikely to get a single-threaded LCL UI to respond in the way you hope it will by interrupting the grid cell selection mechanism.

 

TinyPortal © 2005-2018