Alex Rivera | Logout

Switching from ListView to VirtualStringTree

Asked 2011-01-03T17:38:56.880
9

I am trying to build my projects with a VirtualStringTree rather than a Listview, because of the vast speed difference. The thing is, even after looking thru the demo's, I just can't figure out exactly how I would use it as a ListView. Like, adding, deleting, and basically just working with ListView items is so easy, but when I look at the VT, it gets almost too complicated.

All I am looking for, is a VT that looks like a ListView, with subitems etc.

Here are some routines using the ListView, that I would like to use with VT (This is just a pseudo example:

procedure Add;
begin
  with ListView.Items.Add do
    Begin
      Caption := EditCaption.Text;
      SubItems.Add(EditSubItem.Text):
    End;

end;

Procedure ReadItem(I : Integer);
begin

   ShowMessage(ListView.Items[I].Caption);
   ShowMessage(ListView.Items[I].SubItems[0]);

end;

Of course, also the Delete function, but since thats like 1 line, I didnt bother :P

Could anyone maybe translate the above examples into using a ListView style VT?

Thanks!

Edit
Report

3 Answers

11

Why don't you use a list view in virtual mode? That will look and feel right and perform great.

The Delphi TListView control is a wrapper around the Windows list view component. In its default mode of operation copies of the list data are transferred from your app to the Windows control and this is slow.

The alternative to this is known as a virtual list view in Windows terminology. Your app doesn't pass the data to the Windows control. Instead, when the control needs to display data it asks your app for just the data that is needed.

The Delphi TListView control exposes virtual list views by use of the OwnerData property. You'll have to re-write your list view code somewhat but it's not too hard.

I also offer a link to another question here that covered similar ground. Rather oddly, the accepted answer for that question talked about list boxes even though the question was about list view controls.

answered 2011-01-03T17:53:18.420
6

with VirtualStringTree it's a bit more complex than the simple TListView, however here's a very simple tutorial that I've created a little while back on how to use VirtualStringTree http://www.youtube.com/watch?v=o6FpUJhEeoY I hope it helps, cheers!

answered 2011-01-03T18:34:39.057
1

Get the VT Contributions pack and check out some of the descendants of virtual string tree. That are in there. I haven't used them in projects, but they seem to make Virtual String Tree easier to use.


Here's my getting started primer nonetheless:

I've found after using Virtual String Tree quite a bit that the only way you can make the most of it is by implementing the init node/child functions and setting the root node count, much the same as you would a list view with ownerdraw := true.

It's pretty easy to do stuff with VirtualStringTree, you just need to implement the get text function and the node size functions (set it equal to the size of whatever record you'd like to use as the data behind your tree)

I've found it's almost always easier to do TVirtualTreeNodeRecordData = record Data : TVirtualTreeNodeData; end

and create the data object on the init functions. It creates the pointers for you, but you need to free the objects (again, use another delete node callback).

answered 2011-01-03T20:51:35.367

Your Answer