中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>控件开发>控件使用>文章内容

delphi The New Virtual List Box in Delphi 6

来源:国外 关于:Alec Bergamini 发布时间:2007-07-21   [收藏] [推荐]
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.

[浏览: 次]   
上一篇:delphi VCL控件机制与VCL.net控件机制   下一篇:delphi 组件用户自定义设计期工具
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi treeview 使用
·delphi delphi下对象类别检查
·delphi 组件序列化
·delphi 如何在DBGRID里添加行序号
·delphi 组件用户自定义设计期工具
·delphi 显示GRID单Cell框的Hint
·delphi 如何保存属性到运行期使用(上)
·delphi 组件序列化
·delphi 如何将系统所有菜单列到树上Tre
·delphi 如何将属性在设计期保存到DFM文
·delphi 如何保存属性到运行期使用(下)
·delphi VCL控件机制与VCL.net控件机制
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932