Using changing event of list view to validate data,and setting focus back to the earlier selection if validation fails.

Hi,

I am trying to use the Changing event of list view to validate data entry. If the validation fails the change in selection should not be allowed. But i am getting into problems while using this event. Please help me out to implement this with an example.

Regards, Ravindra
brravi_74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mocartsCommented:
i have this tested with no problem (access is allowd only on first four items):

procedure TForm1.ListView1Changing(Sender: TObject; Item: TListItem;
  Change: TItemChange; var AllowChange: Boolean);
begin
  if Item.Index > 3 then AllowChange := False;
end;

wbr, mo.
0
brravi_74Author Commented:
Hi,

My actual problem is that in the changing event i have written a validation function which should be called once during the change. The function return decides whether the change should be allowed or not. This function is getting called twice/thrice. How can i sort out this?

Regards, Ravindra
0
mocartsCommented:
I don't know why listView sends Selected Item twice before changing..
but puting in the begining of event handler code belov solves your problem:

if Item = TListView(Sender).Selected then exit;

i.e.
procedure TForm1.ListView1Changing(Sender: TObject; Item: TListItem;
 Change: TItemChange; var AllowChange: Boolean);
begin
 if Item = TListView(Sender).Selected then exit;
 if Item.Index > 3 then AllowChange := False;
end;

wbr, mo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mocartsCommented:
now I know why.. it's because ListView uses this event as notification about State change on Selected item, too
so it first notifies that Selected is going to change the state after that it notifies that Selected is going to loose Focus and, at last, notifies which Item will be Selected and asks for accept.

wbr, mo.
0
CleanupPingCommented:
brravi_74:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.