Avatar of ray_marik
ray_marik
 asked on

Listview and Imagelist item deletion

I have a listview and and imagelist on my form, the listview is set to display large icons from left to right. The problem is that I want to set  a limit on how many items the listview can display at a time, say 10. I want to just pass a bitmap to this listview procedure, and it can internally add it and delete the first one if the total number of icons is already 10. This procedure works well up to 11, but then it starts adding icons not to the end of the listview but the beginning in increasing numbers. I've tried everything but can't seem to get it right.

procedure TForm1.FormCreate(Sender: TObject);
begin
  ImageList1.Height := 200;
  ImageList1.Width := 200;
  // initialize LV
  ListView1.Left := 8;
  ListView1.Top := 12;
  ListView1.Height := 244;
  ListView1.Width := 445;
  ListView1.Color := clActiveCaption;
  ListView1.ColumnClick := False;
  ListView1.IconOptions.Arrangement := iaLeft;
  ListView1.ShowColumnHeaders := False;

  ListView1.LargeImages := ImageList1;
  ListView1.LargeImages.Height := 200;
  ListView1.LargeImages.Width := 200;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  bmp: TBitmap;
begin
  OpenDialog1.Filter := 'Bitmaps (*.bmp)|*.BMP';
  if OpenDialog1.Execute then
  begin
    if OpenDialog1.FileName <> '' then
    begin
      bmp := TBitmap.Create;
      try
        bmp.LoadFromFile(OpenDialog1.FileName);
        pushOntoListView(ImageList1, ListView1, bmp, OpenDialog1.FileName);
      finally
        bmp.Free;
      end;

    end;
  end;
end;

procedure pushOntoListView(IL: TImageList; LV: TListView; bmp: TBitmap; inputStr: String);
const
  Max_Count = 10;
var
  index: integer;
  ListItem: TListItem;
begin
  if LV.Items.Count >= Max_Count then
  begin
    //IL.Delete(0);
    LV.Items.Delete(0);
  end;

  // load bitmap into ImageList
  index := IL.Add(bmp, nil);
  // load ImageList into ListView
  if index > -1 then
  begin
    with LV do
    begin
      ListItem := Items.Add;
      Listitem.Caption := inputStr;
      ListItem.ImageIndex := index;
    end;
  end;
end;
Delphi

Avatar of undefined
Last Comment
ray_marik

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ephraim Wangoya

try

procedure pushOntoListView(IL: TImageList; LV: TListView; bmp: TBitmap; inputStr: String);
const
  Max_Count = 10;
var
  index: integer;
  ListItem: TListItem;
begin
  LV.Items.BeginUpdate;
  try
    if LV.Items.Count >= Max_Count then
    begin
      //IL.Delete(0);
      LV.Items.Delete(0);
    end;

    // load bitmap into ImageList
    index := IL.Add(bmp, nil);
    // load ImageList into ListView
    if index > -1 then
    begin
      ListItem := LV.Items.Add;
      Listitem.Caption := inputStr;
      ListItem.ImageIndex := index;
    end;
  finally
    LV.Items.EndUpdate;
  end;
end;

Open in new window

ray_marik

ASKER
Thank you both for your solutions.
However, it seems specifying "LV.Arrange(arAlignLeft)" is the key which is why I rewarded sinisav.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck