Recent

Author Topic: How to avoid autoadvance in TStringgrid  (Read 3562 times)

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
How to avoid autoadvance in TStringgrid
« on: October 20, 2013, 09:18:21 am »
If the validation of a cell entry proves wrong (StringGrid1ValidateEntry(sender: TObject; aCol, aRow: Integer; const OldValue: string; var NewValue: String); )
how to hinder advance to next cell?
I could set NewValue := OldValue; and nothing bad happens. But for the user interface staying in the cell would be more natural; giving the user a change to correct the mistake.
Searching this forum gives the advice about raising an exception; which by others is not recommended ::)

Any hint is highly welcomed?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to avoid autoadvance in TStringgrid
« Reply #1 on: October 20, 2013, 09:43:08 am »

how to hinder advance to next cell?


AFAIK through AutoAdvance property.

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
Re: How to avoid autoadvance in TStringgrid
« Reply #2 on: October 20, 2013, 09:49:00 am »
I am afraid I tried this too
Code: [Select]
    Begin // Does account exist?
      Val(NewValue,I,Code);
      If Code <> 0 Then
        Begin
          MessageDlg('Account is not a number!',mtInformation,[mbOk],0);
          StringGrid1.AutoAdvance:=aaNOne;
          Abort;
This code does not give the wanted effect.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: How to avoid autoadvance in TStringgrid
« Reply #3 on: October 20, 2013, 09:55:31 am »
I think in case of an error, you would have to set the TStringGrid.Cell[OldCol,OldRow]
Lazarus Trunk / fpc 2.6.2 / Win32

MoellerCLaus

  • Full Member
  • ***
  • Posts: 114
    • Vig Foreningsprogram
Re: How to avoid autoadvance in TStringgrid
« Reply #4 on: October 23, 2013, 07:06:37 pm »
Not quite, but I found a solution - happy to hear about other ways of handling this.

In the Grid.ValidateEntry
Code: [Select]
  If      aCol = KasBL Then
    Begin // Check if number
      Val(NewValue,I,Code);
      if Code <> 0 Then Raise Exception.CreateFmt('Number not correct : ''%s''', [newvalue]);
    end

and then create an application exception handler

Code: [Select]
  Application.OnException := @clmExceptionHandler;

Where the handler looks like this

Code: [Select]
procedure TMainForm.clmExceptionHandler(Sender: TObject; E: Exception);
Begin
  MessageDlg(E.Message,mtInformation,[mbOk],0);
end;

 

TinyPortal © 2005-2018