Hi everyone,
I have a ListBox on my form and I give the user an option to add and remove items from the ListBox; here's the code to remove items:
procedure TForm1.ButtonRemoveClick(Sender: TObject);
var
x: integer;
begin
if ListBox1.SelCount > 0 then
begin
for x := ListBox1.Items.Count - 1 downto 0 do
if ListBox1.Selected[x] then
ListBox1.Items.Delete(x);
end;
end;
This code works well except when there are multiple items in the ListBox - once an item is deleted, no items have selection, and you must manually select another item in the ListBox to remove it. I want it so that the ListBox works how most ListBoxes work, in that once the item is deleted, another item becomes selected (usually the one before/above the one that was deleted. I want it so that, if there are items in the ListBox, it is impossible for no items to be selected (because this will cause issues with my program).
I tried this, but it didn't work:
a := ListBox1.ItemIndex;
If ListBox1.ItemIndex > -2 then
begin
ListBox1.Selected[a - 1] := True;
end;
Does anyone have any ideas?
Thanks in advance!