[SOLVED]TstringGrid input validation lazarus / 31-LCL ---------------------------------------------------------------------- Switcher · 2012-01-27 08:47 Hello, I am using TSringGrid To get the run parameters from the user. The user input is limited and validated depending on the value in the left cell (aCol-1) . A possible input is a string witch has to be converted to a real. If the string is not convertible the user gets a message and a standard value is placed in the cell. On top of it I would like to have the focus in the cell witch has to be corrected. I always end up in the next cell (aCol+1) The problem code is in the Validate entry procedure in the except part of the try statement. The commented out lines (//) d'ont help or make things worse (like a never ending loop) Lazarus : 0.9.30 fpc : 2.4.2 xpsp3 anyone suggestions? Code: [Select]procedure TForm1.EventTableSelectEditor(Sender: TObject; aCol, aRow: Integer;   var Editor: TWinControl); var   dozin:string; begin      with Editor as TCustomComboBox do begin        case aCol of        1:begin             Style := csDropDownList  ;          end;        2:begin             dozin := EventTable.Cells[aCol-1,arow];             case StringCase(dozin,['AtOnce', 'EventTime', 'RunTime', 'WaitFor', 'WaitForUntil']) of              0:begin                       Style := csDropDownList  ;                       Items.CommaText:= 'none';                end;              1: Editor := EventTable.EditorByStyle(cbsEllipsis);              2: Editor := EventTable.EditorByStyle(cbsEllipsis);              3,4:begin                       Style := csDropDownList  ;                       Items.CommaText:= '1,2,3,4,5';                  end;             end; // end case evzin          end;        3:begin             Style := csDropDownList  ;          end;        4:begin          end;        end;  //end case aCol      end; //end with as do end; procedure TForm1.EventTableValidateEntry(sender: TObject; aCol, aRow: Integer;   const OldValue: string; var NewValue: String); var   dozin:string;   retint:extended;   msgstr:string; begin   dozin := EventTable.Cells[aCol-1,aRow];   vFlag := true;   case aCol of     1,3: if OldValue <> NewValue then EventTable.cells[aCol+1,aRow]:= '';     2: begin          case StringCase(dozin,['AtOnce', 'EventTime', 'RunTime', 'WaitFor', 'WaitForUntil']) of            1,2:begin                 Try                 NewValue := FloatToStrF(StrToFloat(Newvalue),ffFixed,1,3)                 except                   on E : exception do                   begin                    vFlag := false;                    vCol := aCol;                    vRow := aRow;                    ShowMessage(E.Message + ' ' + NewValue);                    //EventTable.AutoAdvance := aanone;                    EventTable.Cells[aCol,aRow]:='0,0';                    //NewValue:='1,1';                    EventTable.Row:=aRow;  // here it goes wrong                    EventTable.Col:=aCol;                    EventTable.SetFocus;                   end;                 end;                end;              4: ;//validate non picklist vallues;            end;          end;        end;   end; ---------------------------------------------------------------------- ludob · 2012-01-27 09:40 In case of error, store col + row to a variable and do the move in OnSelection: Code: [Select]private   errcol,errrow:integer; ... procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: Integer); begin   if errcol<>0 then     begin     StringGrid1.Col:= errcol;     StringGrid1.Row:= errrow;     errcol:=0;     end; end; procedure TForm1.StringGrid1ValidateEntry(sender: TObject; aCol, aRow: Integer;   const OldValue: string; var NewValue: String); begin ... except   errcol:=aCol;   errrow:=aRow; ... end; ---------------------------------------------------------------------- tintinux · 2012-01-27 09:54 Hi I am not sure to understand : QuoteOn top of it I would like to have the focus in the cell witch has to be corrected. I always end up in the next cell (aCol+1).Do you want to say : When there is an error, stay in the erroneous cell, else move to the next one ? If I'm right and you want to stay in a cell, after a validation error, you only have to raise an exception. No need to assign anything to AutoAdvance, Row,Col or to SetFocus. This is that simple with 0.9.30 ---------------------------------------------------------------------- Switcher · 2012-01-27 10:47 Thanks ludob. The onselection event does the job well . I tried the 'raise exeption' approach as tintinux snuggest  before , it works but I did not like the message style and behavior ---------------------------------------------------------------------- tintinux · 2012-01-27 10:58 If you don't want the default raise message, you can call MessageDlg or ShowModal your customized Form, AND follow by Abort. An utility procedure can do this and be called anywhere it is requested. ---------------------------------------------------------------------- ludob · 2012-01-28 10:48 Quote from: tintinux on January 27, 2012, 11:58:29 amIf you don't want the default raise message, you can call MessageDlg or ShowModal your customized Form, AND follow by Abort. An utility procedure can do this and be called anywhere it is requested. To be honest, this was also my first thought. However, raising exceptions in events is leaving your destiny in the hands of the LCL. I wouldn't bet on the LCL winding back correctly all changes made to the component state when an exception is raised, or even acting upon the exception. Fe. a few days ago I was looking at the OnShow event in TFileDialog. On XP this is called from a callback routine without being encapsulated in a try except clause. Raising an exception in Onshow sends the exception straight into the windows common dialogs dll... All bets are off To be on the safe side, I avoid using exceptions when I don't know who is going to catch them. Events is such a case.