Link to home
Start Free TrialLog in
Avatar of advance1
advance1Flag for United States of America

asked on

How does validation work in the Valuelisteditor Delphi

How can i set off validation in the valuelist editor.
Some fields i want  to validate others i wont
I ask this question to understand validation in the valuelisteditor.
In my program there is a double validation. And I dont want this !

Its for me hard to understand how it works, can someone also give some examples.

Henk
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

You can have a first level of (auto) validation by setting an EditMask property for the ItemProp associated to each entry :
property ItemProps [const KeyOrIndex: Variant]: TItemProp;

for example :
ValListEdt.ItemProps['anHour'].EditMask:='!90:00:00;1;_';  (see TEditMask for help on edit masks)

If you want to further validate values, you use the onValidate event :

TOnValidateEvent = procedure (Sender: TObject; ACol, ARow: Longint; const KeyName, KeyValue: string) of object;

knowing the row, or the keyName, you can determine what kind of validation is required.

ex :

procedure TForm1. ValListEdtValidate(Sender: TObject; ACol, ARow: Longint; const KeyName, KeyValue: string) ;
Var
 H,M,S:Integer;
begin
 if KeyName='anHour' Then
  begin
// KeyValue in the format '90:00:00'
// Here, you don't have to trouble with the fact that it the mask ensure already some validations.
// so each StrToInt should be able to run without exceptions. If not, well that doesn't matter as 
// raising exceptions is the way to break validation
   H:=StrToInt(Trim(Copy(KeyValue,1,2)));
   if H>23 Then Raise Exception.Create('Invalid Hours');
   M:=StrToInt(Trim(Copy(KeyValue,4,2)));
   if M>59 Then Raise Exception.Create('Invalid Minutes');
   S:=StrToInt(Trim(Copy(KeyValue,7,2)));
   if S>59 Then Raise Exception.Create('Invalid Seconds');
  end;
end;

Open in new window

Avatar of advance1

ASKER

The problem was that I handled  the errors first with the raise of an exception and then handle the exception and show a Message, The result was that a second exception exits with a second message box (I don’t know why this happens ?).
I solved this situation by setting a global variable ( Boolean: Checked) to  'False' (when the value changes (on string change)),
When I did the validation I set global variable back to 'True' .
This works but I don’t like this: why is there no variable defined by Delphi to keep track of this situation  (to prevent  further validation, for example:  Cancel)?
Another problem was how to handle the focus for error handling  I used the following solution to prevent  moving of error field:
https://www.experts-exchange.com/questions/21231717/Help-With-A-TValueListEditor-and-OnValidate.html
This works but what I want was when  I check I want to check the Key Name and The Key value, depending of the kind error I want to place the focus to the Key name or the Key value, I
t can also be that the Key name and the Key value are wrong, I want than to edit both fields without getting first an error (ok  I can set a second variable to set off all validation, but I don’t want this),
The next problem I have: I want to set a maximum length of the Key name
And I do want to not to allow the user to enter more positions (maybe I overlooked something, but I cannot find it ?
Thanks  for  thinking with me
Henk
I found out that its easy to change the row of the valuelisteditor  f.e. valuelisteditor1,row =1  after this set focus valueeditor.setfocus.

I think you cannot set maxinumlengh of the keyname. I checked it by code,

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have not read all of your findings attentively enough to say for sure my answers could have helped you finding your solution, but really does it hurt to give points even for a partial help ?
Thks epasquier, for your answer.

I have not tested your last  solution  and as u see i find much out by myself.
to supply to  first answer I Think its better to handle the exception by a  sending a messagebox.
In this case you have to prevend double validation.

The problem was more how to prevend double validtion, how to set off validation,
I think also  (in relation to the link i supplied) that its better in case of an error to only make the errorcolumn editable (selectable)

i will come back to your last answer later.
Where im now looking for is how to select a column of valuelistditor select (select in .net)

Of course i will give your points.


I think answers #33181629 and #33244111 both add some solutions to the problems I understood from what advance1 expressed .
It's hard to tell exactly how this helped and what is missing because there was no code attached.
His original comment on 'double validation' is not clear enough, neither is his large comment on exception handling and global variable. He said himself that a global variable to manage this was not a perfect solution, but again, without source code, it's impossible to help more.

If I can say one thing to advance1, it's to not be afraid to publish some of the code, it will help experts to provide better global solutions instead of pieces of advices on details.
Themordaris, I had not time to validate the answer, of the maximum length, i did not know that it is expected that you answer within 6 days. I want a full understanding before I end the question.

epasquier:
I like to discuss possible improvements
To get other solutions I wil show the code sofar (within a few days, i think tomorrow)
I hope that there are no  misunderstandings

About the select problem: setfocus solves a lot (in my case i get a problem with empty fields in editable fields, the field is then not selected)



arf, you see I will be leaving friday for 2 weeks of vacation...
And if you post your code now, I will have completely forgotten it in 20 days when I'll be around again.
So if you are satisfied with your current solution, just close this question, and maybe in late august, when you will have thought through your code and have more precise question, you can open another thread - that way I'll see it pop in my email.
I have also soon vacation, I will be back in september.

I will then open another thread  with code.

The maximum length solution works.




Thermorduris
PAQ the question and store it in the knowledgebase,

You mave give epasquier 100 pointss for the maximum length answer
His first answer  ID: 33181629 was not new to but found it interesting so i give 50 points for.

Most i have explained ( i will ask in a new thread if somebody could improve it)