Recent

Author Topic: Select previous item in ListBox after deleting selected item  (Read 984 times)

Skaliwop

  • Newbie
  • Posts: 4
Select previous item in ListBox after deleting selected item
« on: December 05, 2019, 12:10:54 am »
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:

Code: Pascal  [Select]
  1. procedure TForm1.ButtonRemoveClick(Sender: TObject);
  2. var
  3.   x: integer;
  4. begin
  5.  
  6. if ListBox1.SelCount > 0 then
  7.   begin
  8.     for x := ListBox1.Items.Count - 1 downto 0 do
  9.       if ListBox1.Selected[x] then
  10.         ListBox1.Items.Delete(x);
  11.   end;              
  12. 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:

Code: Pascal  [Select]
  1.   a := ListBox1.ItemIndex;
  2.   If ListBox1.ItemIndex > -2 then
  3.   begin
  4.         ListBox1.Selected[a - 1] := True;
  5.  
  6.   end;  

Does anyone have any ideas?
Thanks in advance!

winni

  • Hero Member
  • *****
  • Posts: 590
Re: Select previous item in ListBox after deleting selected item
« Reply #1 on: December 05, 2019, 12:22:37 am »
Hi!

If ListBox.count < 256
   use a set
else
  use an array of integer

Loop 1: Look which item is selected and notice it in the set or array
Loop 2: Loop the set/array and delete the related item.

Winni

wp

  • Hero Member
  • *****
  • Posts: 6471
Re: Select previous item in ListBox after deleting selected item
« Reply #2 on: December 05, 2019, 12:36:47 am »
I normally do it this way:

Store the current ItemIndex. Delete the item. Then set the ItemIndex to the stored value, but make sure that it was not the last item that was deleted.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   idx: Integer;
  4. begin
  5.   idx := Listbox1.ItemIndex;
  6.   if idx = -1 then
  7.     exit;
  8.   Listbox1.Items.Delete(idx);
  9.   if idx < Listbox1.Count then
  10.     Listbox1.ItemIndex := idx
  11.   else
  12.     Listbox1.ItemIndex := Listbox1.Count - 1;
  13. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 3201
Re: Select previous item in ListBox after deleting selected item
« Reply #3 on: December 05, 2019, 12:48:40 am »
An alternative is this:
Code: Pascal  [Select]
  1. procedure DeleteSelectedListBoxItems(aListBox: TListBox);
  2. var
  3.   i, last: Integer;
  4. begin
  5.   if (not Assigned(aListBox)) or (aListBox.Items.Count = 0) or (aListBox.SelCount = 0) then
  6.     Exit;
  7.   for i := aListBox.Items.Count - 1 downto 0 do
  8.     if aListBox.Selected[i] then
  9.       begin
  10.         aListBox.Items.Delete(i);
  11.         last := i;
  12.       end;
  13.   if (aListBox.Items.Count > 0) then
  14.     aListBox.Selected[last] := True;
  15. end;

Skaliwop

  • Newbie
  • Posts: 4
Re: Select previous item in ListBox after deleting selected item
« Reply #4 on: December 05, 2019, 12:57:16 am »
Thank you all for your replies, it's working as it should now!