Listview Loop Problem (Urgent)

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 !!!

stevencool2000Asked:
Who is Participating?
 
mokuleCommented:
MY FAULT ERROR IN COPY

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]);
///// not here    end;
 Listview1.items.delete(0);
 OBJ_MainForm.Refresh;     // Refreshes Edit boxes or whatever Your form's name is
end;

0
 
mokuleCommented:
You should start deleteing from the end.
Or delete always Item[0]
0
 
mokuleCommented:
while  ListView1.Items.Count > 0  do
  begin
   ID.Text:=( ListView1.Items[ 0 ].Caption);
......
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
mokuleCommented:
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;

0
 
stevencool2000Author 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 :(

 
0
 
stevencool2000Author 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 :(
0
 
mokuleCommented:
Should be in this order

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

What does that mean it's not moving?
0
 
mokuleCommented:
I think it is not necessry

 ListView1.Refresh;

What do You mean not working. I've tested it and it's OK.
0
 
stevencool2000Author 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!!!
0
 
stevencool2000Author 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.
0
 
mokuleCommented:
Please send Your code once more. It's not possible
0
 
mokuleCommented:
You must refresh Your Form or Edit boxes not ListView
0
 
stevencool2000Author 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;
0
 
mokuleCommented:
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;

0
 
stevencool2000Author 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.
0
 
mokuleCommented:
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
0
 
stevencool2000Author Commented:
I've tried what you pasted and nothing is deleted from the listview and the application get's stuck :(

0
 
mokuleCommented:
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;
0
 
stevencool2000Author Commented:
Sorry, but it doesn't work :(
0
 
mokuleCommented:
Is it OK now after deleting this
fatal end; ?
0
 
stevencool2000Author Commented:
It now worked but it took all of them in one go...let me try one thing please, 1 minute

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

Tanks a lot for you time and help.

Here are your points :)

Thanks again !!!
0
 
mokuleCommented:
Happy to help :)
0
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.

All Courses

From novice to tech pro — start learning today.