Forum > LCL

[SOLVED] How to stay in the invalid cell in Event TStringGrid_OnEditingDone?

<< < (2/3) > >>

dsiders:

--- Quote from: egsuh on December 06, 2023, 06:48:06 am ---I think there should be someway to cancel or abort following operations at the position of comment, because moving to another cell (when an arrow is clicked) is performed after EditingDone event is handled. 
abort does not work.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.StringGrid1EditingDone(Sender: TObject);var   r, c: integer;begin   r := StringGrid1.Row;   c := StringGrid1.Col;    if StrtoIntDef(StringGrid1.Cells[c, r], -1) = -1 then begin      ShowMessage('Only number please');      StringGrid1.Cells[c, r] := '';       StringGrid1.Row := r;      StringGrid1.Col := c;       // Someway to cancel following actions   end;end;
--- End quote ---

Have you noticed or tried OnValidateEntry?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.StringGrid1ValidateEntry(Sender: TObject; aCol, aRow: Integer;  const OldValue: string; var NewValue: String);var  IntVal: Integer;begin  if (ACol = 1) and (not TryStrToInt(NewValue, IntVal)) then  begin    NewValue := OldValue;    raise Exception.Create('Only numeric values in column 1');  end;end;
[ edit ]Sorry, I replied to the wrong person.[/ edit ]

wp:
dsiders is right. Let me explain: OnValidateEntry is fired from the boolean function TCustomGrid.ValidateEntry:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TCustomGrid.ValidateEntry(const ACol, ARow: Integer;  const OldValue:string; var NewValue:string): boolean;begin  result := true;  if assigned(OnValidateEntry) then begin    try      OnValidateEntry(Self, ACol, ARow, OldValue, NewValue);    except      on E:Exception do begin        result := false;        if FGridState=gsSelecting then          FGridState := gsNormal;        Application.HandleException(E);      end;    end;  end;end;As can be seen the return value of this function (which decides on further processing) becomes false only when the OnValidateEntry raises an exception. Therefore, you must do this in on OnValidateEntry handler if you consider the current input to be incorrect.

See also: https://wiki.freepascal.org/Grids_Reference_Page#Validating_Entered_Values

Hartmut:
Thank you very much to all for your posts and suggestions. I studied them all and learned from you.

I choosed jamie's solution because this was the 1st one which worked perfectly in my program.

Special thanks to wp for your explanations about Event OnValidateEntry and how it is embedded in TCustomGrid.ValidateEntry and the documentation link.

In the future I will try Event OnValidateEntry which I did not know before.

This is a great Forum!

egsuh:

--- Quote ---Have you noticed or tried OnValidateEntry?
--- End quote ---

I didn't know this. Thanks a lot (to wp as well).

jamie:
Apparently, if you want to use the OValidate event, all one needs to do to reject the cell's contents and stay in place is simply all the "Abort" while in that event.
So if you don't like the new string, you can just "Abort" and it will just stay there in the cell.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version