Forum > LCL

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

(1/3) > >>

Hartmut:
My program has a StringGrid where 2 columns are editable. To validate the user input, I use Event StringGrid1EditingDone. When I detect a bad input, I show a Message and want the Cursor to stay in the invalid cell. How can this be done? Assigning StringGrid1.Col and StringGrid1.Row a value, does not work. The cursor moves always to the new cell, whereto the user moved.

I use Lazarus 2.0.10 with FPC 3.2.0. Thanks in advance.

jamie:

--- 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";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids,LclIntf,Lmessages; type   { TForm1 }   TForm1 = class(TForm)    StringGrid1: TStringGrid;    procedure StringGrid1EditingDone(Sender: TObject);  private   public     Procedure ResetGridCell(Var Msg:TLmessage); Message LM_USER+1;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 }PRocedure TForm1.ResetGridCell(Var Msg:TLmessage);Begin  If TObject(Msg.lParam) is TStringGrid Then With TStringGrid(Msg.Lparam) do   Begin     COL := Lo(DWord(Msg.wParam));     Row := Hi(Dword(Msg.wparam));   end;end; procedure TForm1.StringGrid1EditingDone(Sender: TObject);begin  If Sender is TStringGrid then with TStringGrid(Sender) do  Begin    If Modified then {assumed an error for now as a test}     PostMessage(Self.handle, LM_USER+1, MakeLong(COL,ROW),PtrUint(Sender));    Modified := false;  end;end; end. 
Time to learn some real code  ;D

jamie:
In case you want to enhance your editing mode.


--- 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.ResetGridCell(Var Msg:TLmessage);Begin  If TObject(Msg.lParam) is TStringGrid Then With TStringGrid(Msg.Lparam) do   Begin     COL := Lo(DWord(Msg.wParam));     Row := Hi(Dword(Msg.wparam));     if Editor is TStringCellEditor Then      Begin       EditorMode := true;       TStringCellEditor(Editor).SelLength:=0;      end;   end;end;                                    

egsuh:

https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tcustomgrid.setcolrow.html

There is an explanation of SetColRow method, but this is not compiled. TStringGrid is a descendant of TCustomGrid, but this method is "protected". Is there anyway to call this function, e.g. using class helper, etc.?

egsuh:
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;

Navigation

[0] Message Index

[#] Next page

Go to full version