Dynamic listView

I'd like to create the items in the listView dynamically, and also its images. The image consists of a line with different colours. What is the easiest way to do it? And I also need to clear the list, which call i need to use to perform the clearing?
maulingAsked:
Who is Participating?
 
men xinConnect With a Mentor CEOCommented:
mauling:

I have a simple example for you.There are a Button ,a listview and a imagelist in a form.There are sevrial icons in the imagelist.You can set the listview's largeimage or smallimage link to the imagelist(by your listview's style).
And the codes is:


procedure TForm1.ListView1GetSubItemImage(Sender: TObject; Item: TListItem;
  SubItem: Integer; var ImageIndex: Integer);
//this function can draw listview's subitemimage.
begin
  if SubItem<>2 then
    ImageIndex:=SubItem
  else
    ImageIndex:=-1;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j:Integer;
begin
  with ListView1 do begin
    Items.Clear;
    for j:=1 to 5  do begin
      Items.Add;
      Items[j-1].Caption:=format('item%d',[j]);
      For i:=1 to 4 do begin
        if i<3 then
          Items.Item[j-1].SubItems.Add('') //subitem 1&2 put ''
        else
          Items.Item[j-1].SubItems.Add(Format('%d,%d',[j,i]))
      end;
    end;
  end;
end;


menxin
0
 
KECommented:
To clear the list set Items.Count := 0;

I don't understand what you mean exactly with the image part...
To set the image on the item, use Item.ImageIndex - and for the subitems use SubImageIndex[x];

BTW. Remember to Connect an ImageList to your ListView;

The only thing you need to dynamically drive the imagelist is Items.Count and the OnItemData event. The OnItemData event should apply the Item and SubItems data values (I'm not refering to Item.Data) with reference to the Item.Index.

Regards

0
 
MadshiCommented:
mauling, sounds like a task for the virtual TListView mode. This mode is available in Delphi4 or Delphi5. If you only have D2 or D3, download the TExtListView component from this page:

http://www.delphifreestuff.com/

If you've D4 or D5, look at the "c:\delphi\demos\virtual listview" demo.

Does this help?

Regards, Madshi.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
men xinCEOCommented:
I use Delphi5
0
 
maulingAuthor Commented:
Thanks, Menxin.
0
 
KECommented:
Oh, I just realized that "dynamically" also has another meaning ;-)

Anyway, if you wan't it to be "driven dynamically", or let's call it "as a virtual list", then follow my directions - otherwise I believe that menxin has answered you question.

Regards
0
All Courses

From novice to tech pro — start learning today.