Link to home
Create AccountLog in
Avatar of advance1
advance1Flag for United States of America

asked on

Can this code improved ?

Hello,

Following the discussions of july and august and want to start a new topic.
Prior related question:
https://www.experts-exchange.com/questions/26321089/How-does-validation-work-in-the-Valuelisteditor-Delphi.html

I used the valuelisteditor for an application to add a location.
Going on and with help of experts i improved the code.

Two open issues:
1. The problem was that u must must use many booleans to make it work comform specifications.
2. I recognized that the setfocus method  not always select the field (make green on my computer)
    How can I make the error field selectable also when the cursor is on an other control ? (it happens  when   the focus is on button1)

Other comments are also welcome.

Here is code sofar ( I changed it a little )


procedure TForm1.ValueListEditor1SelectCell(Sender: TObject; ACol,
   ARow: Integer; var CanSelect: Boolean);
   begin
     if valid = false  then
     begin
       if ((ACol = ErrorCol) and (Arow = ErrorRow)) or (closing) then
          canselect:= true
       else
          canselect := false;

       button1.enabled:=false;
     end
     else
     begin
       CanSelect := true;
       button1.enabled:= true
     end;

     if closing then
        button1.enabled:=true
   end;



procedure TForm1.ValueListEditor1StringsChange(Sender: TObject);
begin
// check only as strings change and not close
   if not Closing
     then
       Checked:=False
     else
       Checked:=True;

end;


Function SetErrorCaption (ACol:Integer;ARow:Integer):string;
var Caption:String;
begin
   Form1.ValueListEditor1.Col:= ACol;
   Form11.ValueListEditor1.Row:= ARow;

   ErrorCol:= ACol;
   ErrorRow:= ARow;

   if Acol = 0 then
       Caption:= 'Locatie name not valid';

   if Acol = 1 then
       Caption:= 'number of places not valid';


   Agendaform1.Valuelisteditor1.Setfocus;

   result:= Caption;
end;

procedure TForm1.ValueListEditor1Validate(Sender: TObject; ACol, 
ARow: Integer;
const KeyName, KeyValue:string);

var KeyValueInt,PreviousValue:Integer; ErrorCaption:string;
begin

Try
if Checked= false then
begin

    Checked:=True;

    if trim (Keyname) = '' then
       begin
         ErrorCaption:= SetErrorCaption(0,Arow);
         raise Exception.create ('Locatiename has no valid value') 
       end;

     if tryStrtoInt (Trim(KeyValue),KeyValueInt) then
     begin
       if (KeyValueInt < 0) or (KeyvalueInt > 999)  then
         begin
           ErrorCaption:= SetErrorCaption(1,Arow);
           raise Exception.create('errormessage2');
         end
   
   
end;//checked
    // on exception show messagebox
    except
      on E : Exception do
       begin
         Windows.MessageBox(0,pWideChar(E.Message),PwideChar(ErrorCaption),mb_Ok);
         valid:=false;
       end;
    end; //except

end; //procedure

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
thanks for the move of the code, it was almost unreadable before
Avatar of advance1

ASKER


Thks for your comments  Good that you noticed,
I changed AgendaForm1 to Form1. So There is only 1 form (Form1).

I will check your changes.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thks for the comments.
I think the comments are correct.
Its the old discussion is more cryptic code better code. I dont think this always so,

About the selectmode  I want the text of this cell is selected in edit mode,
I set the EditorMode property to True after the SetFocus, but when I clicked on button1 the error field is still not selected (This happens when I click the mouse).


you mean it's in edit mode, but not selected ?

I'm not sure that can be done (easily). I'll check that tomorrow if you confirm that is what you want  
Yes, I mean that.
Thks in advance !
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Sounds good , I will try it Monday
Yess its ok !!

A lot of code but it works !

Thks.
I keep de question few days  open for other reactions,