We help IT Professionals succeed at work.

Working with Listboxes...finding the last item.

rperies
rperies asked
on
Hi, I have set up a program that can add any given number of entries into a certain listbox. I want to find the itemindex of the last entry and have it as a variable so that I can cycle through the whole list with a forloop and perform operations. For instance, If I had made 10 entries, I would want the variable 'lastnumber' to be assigned to '10' and thereby be able to make a forloop that goes firstnumber to lastnumber. I'm learning Delphi after starting with Visual Basic, and it seems easy enough, I'm just unfamiliar with the syntax :(
Comment
Watch Question

Commented:
Try listBox1.items.count so

for x := 0 to listBox1.items.count-1 do

should work.

GL
Mike
Commented:
Mikes comment is how you do it. If you still prefer to use a variable then you could do it something like this.

var
  iLastIndex: Integer;



iLastIndex := listBox1.items.count - 1;



Since the ListIndex is zero based that is the reason for the - 1.

Count is the actual amount of items in the list so if there is just one item then the count would egual 1 but the ItemIndex would be zero.


The Crazy One

Author

Commented:
Thanks, that was very helpful :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.