Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Listview and Imagelist item deletion

Posted on 2012-03-11
3
Medium Priority
?
941 Views
Last Modified: 2012-06-27
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;
0
Comment
Question by:ray_marik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 37708609
your procedure may looks like (hint: do arrange after delete):

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
      LV.Items.Delete(0);
      LV.Arrange(arAlignLeft);
    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;
  finally
    LV.Items.EndUpdate;
  end;
end;

Open in new window


... another note: you can use custom sort to rearrange items.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 37708621
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

0
 

Author Closing Comment

by:ray_marik
ID: 37711743
Thank you both for your solutions.
However, it seems specifying "LV.Arrange(arAlignLeft)" is the key which is why I rewarded sinisav.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question