Reputation:
I want to make a TLabel caption show the items of a TListbox with regular intervals in a loop.
In Listbox1: TListbox
I have these items:
Item1
Item2
Item3
Item4
and I have a Label1: TLabel
that I want to show each item of Listbox1
in turn for a periode of 4 seconds. After the last item is shown, it should return to show the first one.
I have no idea how to do it but I tried with this code.. but it isn't working
var
i:integer
begin
for i:=0 to Listbox1.Items.count do
Label1.Caption := Listbox1.items[i];
Is there any way to do it?
Upvotes: 2
Views: 332
Reputation: 21045
You need a place to store the index of the displayed item, until it is time to change to the next one. So, create a field of type integer
in the private
section of the form and call it IndexCounter
You also need something to set the pace of how often the label is changed. Drop a TTimer
on the form. Set its Interval
property to 4000 (it counts in milliseconds).
With the added timer selected, switch Object Inspector to show Events, and doubleclick in the entry field for OnTimer
. A procedure for the event is created for you:
procedure TForm1.Timer1Timer(Sender: TObject);
begin
end;
Fill in code to increment the IndexCounter
and use integer division remainder function (mod
) to get a value in the range 0..ListBox1.Count-1.
IndexCounter := (IndexCounter + 1) mod ListBox1.count;
Add code to show the listbox item with index IndexCounter
.
Label1.Caption := ListBox1.Items[IndexCounter];
Upvotes: 2