Recent

Author Topic: [SOLVED]TstringGrid input validation  (Read 8628 times)

Switcher

  • Newbie
  • Posts: 6
[SOLVED]TstringGrid input validation
« on: January 27, 2012, 09:47:20 am »
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;
« Last Edit: January 27, 2012, 11:49:40 am by Switcher »

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TstringGrid input validation
« Reply #1 on: January 27, 2012, 10:40:57 am »
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

  • Sr. Member
  • ****
  • Posts: 373
    • Gestinux
Re: TstringGrid input validation
« Reply #2 on: January 27, 2012, 10:54:50 am »
Hi

I am not sure to understand :
Quote
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).
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

Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

Switcher

  • Newbie
  • Posts: 6
Re: [SOLVED]TstringGrid input validation
« Reply #3 on: January 27, 2012, 11:47:57 am »
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

  • Sr. Member
  • ****
  • Posts: 373
    • Gestinux
Re: [SOLVED]TstringGrid input validation
« Reply #4 on: January 27, 2012, 11:58:29 am »
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.

Initiator of gestinux, open-source, multi-database and multilingual accounting and billing software made with LAZARUS.

You can help to develop, to make and improve translations, and to provide examples of legal charts and reports from more countries.

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED]TstringGrid input validation
« Reply #5 on: January 28, 2012, 11:48:26 am »
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.
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.

 

TinyPortal © 2005-2018