We help IT Professionals succeed at work.

Listview Loop Problem (Urgent)

stevencool2000
on
Medium Priority
241 Views
Last Modified: 2010-04-05
Hi there,

I have the following problem.
I have a listview that imports a list.
What i want to do is to have a loop that will scroll the lists 1 by 1, execute something and then go to the next one in the list etc... until the listbox is empty.

My code is here but it does not work :(

procedure TOBJ_MainForm.LMDButton2Click(Sender: TObject);
var
I: Integer;

Begin

for I:= 0 to ListView1.Items.Count -1  do
begin
 

    ID.Text:=( ListView1.Items[I].Caption);
    Edit9.Text:=( ListView1.Items[I].SubItems[0]);
    Edit10.Text:=( ListView1.Items[I].SubItems[1]);
    Edit11.Text:=( ListView1.Items[I].SubItems[2]);
    Edit12.Text:=( ListView1.Items[I].SubItems[3]);
    SendMailTo.Text:=( ListView1.Items[I].SubItems[4]);


    end;

  inc(I);
 ListView1.Refresh;
 Listview1.items.delete(0);
end;

I've been trying lots of things but nothing seems to work.

I also need it to finish the execution of say the first one and then go into the second one and so on.
I guess i could do that with a boolean expresion.
Most of all i need the loop to work.

Please help

Thanks !!!

Comment
Watch Question

Commented:
You should start deleteing from the end.
Or delete always Item[0]

Commented:
while  ListView1.Items.Count > 0  do
  begin
   ID.Text:=( ListView1.Items[ 0 ].Caption);
......

Commented:
fwhile  ListView1.Items.Count > 0  do
 begin
    ID.Text:=( ListView1.Items[0].Caption);
    Edit9.Text:=( ListView1.Items[0].SubItems[0]);
    Edit10.Text:=( ListView1.Items[0].SubItems[1]);
    Edit11.Text:=( ListView1.Items[0].SubItems[2]);
    Edit12.Text:=( ListView1.Items[0].SubItems[3]);
    SendMailTo.Text:=( ListView1.Items[0].SubItems[4]);
    end;
 ListView1.Refresh;
 Listview1.items.delete(0);
end;

Author

Commented:
The loop is still not right.
It's not moving the item that it has deleted or cleared, it's moving the next one on the row :(

 

Author

Commented:
The While loops are not working :(
Your code would work fine on a listbox but it's not working for a listview for some reason :(

Commented:
Should be in this order

 Listview1.items.delete(0);
 ListView1.Refresh;

What does that mean it's not moving?

Commented:
I think it is not necessry

 ListView1.Refresh;

What do You mean not working. I've tested it and it's OK.

Author

Commented:
Well, say we start with the first line, it gets all the items and puts them in different editboxes.
The problem is that it's not moving the one that it has deleted to the edit boxes, it's showing the next one down :(  Strange!!!

Author

Commented:
It's actually moving the last in the list and erasing a another one.

For example:

I have :  line 1
             line 2
             line 3

It's deleting line 1 and moving line 3 to the edit boxes.

Commented:
Please send Your code once more. It's not possible

Commented:
You must refresh Your Form or Edit boxes not ListView

Author

Commented:
Here it is:

procedure TOBJ_MainForm.Button3Click(Sender: TObject);
var
I: Integer;

Begin

for I:= 0 to ListView1.Items.Count -1  do
begin


    ID.Text:=( ListView1.Items[I].Caption);
    Edit9.Text:=( ListView1.Items[I].SubItems[0]);
    Edit10.Text:=( ListView1.Items[I].SubItems[1]);
    Edit11.Text:=( ListView1.Items[I].SubItems[2]);
    Edit12.Text:=( ListView1.Items[I].SubItems[3]);
    SendMailTo.Text:=( ListView1.Items[I].SubItems[4]);


    end;

  inc(I);
 Listview1.items.delete(0);
 ListView1.Refresh;



end;

Commented:
It must work

while  ListView1.Items.Count > 0  do
 begin
    ID.Text:=( ListView1.Items[0].Caption);
    Edit9.Text:=( ListView1.Items[0].SubItems[0]);
    Edit10.Text:=( ListView1.Items[0].SubItems[1]);
    Edit11.Text:=( ListView1.Items[0].SubItems[2]);
    Edit12.Text:=( ListView1.Items[0].SubItems[3]);
    SendMailTo.Text:=( ListView1.Items[0].SubItems[4]);
    end;
 Listview1.items.delete(0);
 Form1.Refresh;     // Refreshes Edit boxes or whatever Your form's name is
end;

Author

Commented:
Evertime the button is clicked it must transfer the top listview items to the edit boxes and erase itself from the listview...etc...etc...

That's all i need it to do.

Commented:
No.
Please note the line

while  ListView1.Items.Count > 0  do


and

 Form1.Refresh;     // Refreshes Edit boxes or whatever Your form's name is

there is no
Inc(I)
there is no I at all.
It'll be good

Author

Commented:
I've tried what you pasted and nothing is deleted from the listview and the application get's stuck :(

Commented:
It is working.
Do You change Form1 to Your Forms name

while  ListView1.Items.Count > 0  do
 begin
    ID.Text:=( ListView1.Items[0].Caption);
    Edit9.Text:=( ListView1.Items[0].SubItems[0]);
    Edit10.Text:=( ListView1.Items[0].SubItems[1]);
    Edit11.Text:=( ListView1.Items[0].SubItems[2]);
    Edit12.Text:=( ListView1.Items[0].SubItems[3]);
    SendMailTo.Text:=( ListView1.Items[0].SubItems[4]);
    end;
 Listview1.items.delete(0);
 OBJ_MainForm.Refresh;     // Refreshes Edit boxes or whatever Your form's name is
end;
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Sorry, but it doesn't work :(

Commented:
Is it OK now after deleting this
fatal end; ?

Author

Commented:
It now worked but it took all of them in one go...let me try one thing please, 1 minute

Author

Commented:
Great !!! It now works fine :)

Tanks a lot for you time and help.

Here are your points :)

Thanks again !!!

Commented:
Happy to help :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.