Mirror

The New Virtual List Box in Delphi 6 (Views: 101)


Problem/Question/Abstract:

What are the new virtual styles in the TListbox for, and whay are they important?

Answer:

One of the new features in Delphi 6 is the addition of styles lbVirtual and lbVirtualOwnerDraw to the standard TListBox. In all the Delphi 6 “What’s new” articles I’ve read this addition has received no more than a passing mention.

Why are Virtual List boxes important?

Have you ever been frustrated by the limitations of the TStrings' objects property or have had a need to create a custom display string in the list. Sure, you can assign any pointer you want to the object field of a string list but what if you already have a list container (like a Tlist) full of data. Why should you be forced to duplicate this data over to the TStrings associated with the TListBox. You should be able to just use it. Well, with a virtual list box you can.

During the past week I ran into a situation where I had a TInterfaceList and wanted to use the value returned from one of the Interface's functions as the text of the list item. Under Delphi 5 this required that I step down through the TInterfaceList calling the required interface method at each step to add the strings to the TListBox. Then I would need to synchronize any movement and selection in the list box with the TInterfaceList. Under Delphi 6, using a virtual list box, I was able merge my TInterfaceList and the TListbox into one highly usable pseudo object. That is, I was able to make the TListBox “aware” of the TInterfaceList directly.

(BTW, my original thought was to not use the TInterfaceList at all and just place the references to the Interface into the object field of the TStrings member of the list box.. As you all probably know, this was a bad idea since it totally screws the reference counting on the interface.)

Anyway I will not go into great detail about setting up a virtual list box since the Delphi help is pretty good on this. Basically you need to set the style to one of the 2 virtual styles, set the list box’s count to the number of items in the list and then fill in at few as one and as many as 3 events.

Here is a trivial sample that may help to fill in some blanks left by the lack of sample code in the help file.

I only wish that Borland could have also added this feature the other list controls like the comboboxes and the treeview.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Edit1: TEdit;
    lblColor: TLabel;
    lblNumber: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure ListBox1Data(Control: TWinControl; Index: Integer;
      var Data: string);
    function ListBox1DataFind(Control: TWinControl;
      FindString: string): Integer;
    procedure ListBox1DataObject(Control: TWinControl; Index: Integer;
      var DataObject: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ObjList: TList;
  end;

  TMyObj = class
    fColor: string;
    fNumber: Integer;
    constructor create(const color: string; const Number: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TMyObj }

constructor TMyObj.create(const color: string; const Number: Integer);
begin
  fColor := color;
  fNumber := number;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  // create a TList and add some data to it.
  ObjList := TList.Create;
  ObjList.Add(TMyObj.Create('Red', 1));
  ObjList.Add(TMyObj.Create('Yellow', 15));
  ObjList.Add(TMyObj.Create('Blue', 21));
  ObjList.Add(TMyObj.Create('Green', 37));
  ObjList.Add(TMyObj.Create('Brown', 16));
  ObjList.Add(TMyObj.Create('Black', 5135));
  ObjList.Add(TMyObj.Create('White', 4));
  ObjList.Add(TMyObj.Create('Orange', 333));
  // ABSOLUTELY REQUIRED Set the count of the virtual listbox
  Listbox1.Count := ObjList.Count;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to ObjList.count - 1 do
    TMyObj(ObjList.Items[I]).free;
  ObjList.Free;
end;

procedure TForm1.ListBox1Data(Control: TWinControl; Index: Integer;
  var Data: string);
// REQUIRED
// return a string to put in the list box
begin
  Data := TMyObj(ObjList.Items[Index]).fColor;
end;

procedure TForm1.ListBox1DataObject(Control: TWinControl; Index: Integer;
  var DataObject: TObject);
// USUALLY REQUIRED
// return an object associated with the current selection of the list box
begin
  DataObject := ObjList.Items[Index];
end;

function TForm1.ListBox1DataFind(Control: TWinControl;
  FindString: string): Integer;
// USUALLY REQUIRED
// given a string FindString, return its index
var
  I: Integer;
begin
  // the simplest but most brain dead approach
  result := -1;
  for I := 0 to TListBox(Control).Count - 1 do
    if TListBox(Control).Items[I] = FindString then
      result := I;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.ItemIndex := ListBox1.Items.IndexOf(edit1.Text);
  // I don't think this next should be necessary but..
  ListBox1Click(Self);
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  lblColor.Caption := TMyObj(ListBox1.Items.objects[Listbox1.ItemIndex]).fColor;
  lblNumber.Caption :=
    IntToStr(TMyObj(ListBox1.Items.objects[Listbox1.ItemIndex]).fNumber);
end;

end.

<< Back to main page