0
votes

I have a listbox (ListBox1) and a button (Button1). When I click on the button, a new item is added to the listbox. If I click on the newly added item, it is highlighted. If I add another item to the listbox, the previously selected item is still highlighted. I want it so that the item that I add gets highlighted (selected) automatically.

How do I do this?

Thanks in advance.

1

1 Answers

2
votes

Assign the ItemIndex property of the list box. For example...

ListBox1.ItemIndex := ListBox1.Items.Count-1;

The -1 is because lists are 0 based.


Alternatively, you can obtain the index of the new item directly when you call Add(), so...

var
  I: Integer;
begin
  I:= ListBox1.Items.Add('Some Value');
  ListBox1.ItemIndex := I;
end;

Or even more simple:

ListBox1.ItemIndex := ListBox1.Items.Add('Some Value');