Board index » delphi » ListBox question!

ListBox question!

Is it possible to delete all items in a Listbox which have the same
name? IE all items thats named A? Thanks in Advance.
//Johan
 

Re:ListBox question!


Quote
On Sun, 23 Jul 2000 15:17:42 +0200, Johan <east...@swipnet.se> wrote:
> Is it possible to delete all items in a Listbox which have
> the same name?

Sure, use TListBox.Items.Delete to delete specific items.
--
Rick Rogers (TeamB)
www.fenestra.com and www.componentfactory.com

Re:ListBox question!


Quote
> Is it possible to delete all items in a Listbox which have the same
> name? IE all items thats named A? Thanks in Advance.

Something like this maybe:

  index:=listbox1.items.indexof('a');
  while index>=0 do
  begin
    listbox1.Items.Delete(index);
    index:=listbox1.Items.IndexOf('a');
  end;

johannes
--
Please reply in this newsgroup only
 - SIP solutions -
http://www.sipsolutions.de/

Re:ListBox question!


Hi again.
Now I have another problem, how do I delete one 'A' from a list of many
'a's but only one and it doesn't matter which? Thanks in Advance.
Quote
Johannes Berg wrote:
> > Is it possible to delete all items in a Listbox which have the same
> > name? IE all items thats named A? Thanks in Advance.

> Something like this maybe:

>   index:=listbox1.items.indexof('a');
>   while index>=0 do
>   begin
>     listbox1.Items.Delete(index);
>     index:=listbox1.Items.IndexOf('a');
>   end;

> johannes
> --
> Please reply in this newsgroup only
>  - SIP solutions -
> http://www.sipsolutions.de/

Re:ListBox question!


Quote
> Now I have another problem, how do I delete one 'A' from a list of many
> 'a's but only one and it doesn't matter which? Thanks in Advance.

Well, if it doesn't matter which use this:

  index:=listbox1.items.indexof('a');
  if index>=0 then listbox1.items.delete(index);
if you want to drop a random one, then you need to cycle through, build a list
of the items that are 'a' and then drop a random one...
(untested code)

var
  items : array of integer;
  i : integer;
begin
  for i:=0 to listbox1.items.count-1 do  // cycle through
   if listbox.items[i]='a' then begin   // found
     setlength(items,length(items)+1); // inefficient but quick to code
     items[high(items)]:=i;           // save this item
   end;
  if length(items)>0 then begin
    listbox1.items.delete(items[random(length(items))]);
    // select random entry from the items array,
    // and delete the item it points to
  end;
end;

johannes
--
Please reply in this newsgroup only
 - SIP solutions -
http://www.sipsolutions.de/

Other Threads