在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
http://users.atw.hu/delphicikk/listaz.php?id=2471&oldal=52 Problem/Question/Abstract: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ovcbase, ovcviewr, Vcl.StdCtrls; type TForm3 = class( TForm ) ListBox1 : TListBox; Button1 : TButton; lblNumber : TLabel; lblColor : TLabel; Edit1 : TEdit; procedure FormCreate( Sender : TObject ); procedure FormDestroy( Sender : TObject ); procedure ListBox1Data( Control : TWinControl; Index : Integer; var Data : string ); procedure ListBox1DataObject( Control : TWinControl; Index : Integer; var DataObject : TObject ); function ListBox1DataFind( Control : TWinControl; FindString : string ) : Integer; 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 Form3 : TForm3; implementation {$R *.dfm} { TMyObj } constructor TMyObj.create( const color : string; const Number : Integer ); begin fColor := color; fNumber := Number; end; procedure TForm3.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 TForm3.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 TForm3.FormDestroy( Sender : TObject ); var I : Integer; begin for I := 0 to ObjList.Count - 1 do TMyObj( ObjList.Items[ I ] ).free; ObjList.free; end; procedure TForm3.ListBox1Click( Sender : TObject ); begin lblColor.Caption := TMyObj( ListBox1.Items.objects[ ListBox1.ItemIndex ] ).fColor; lblNumber.Caption := IntToStr( TMyObj( ListBox1.Items.objects[ ListBox1.ItemIndex ] ).fNumber ); end; procedure TForm3.ListBox1Data( Control : TWinControl; Index : Integer; var Data : string ); begin Data := TMyObj( ObjList.Items[ index ] ).fColor; end; function TForm3.ListBox1DataFind( Control : TWinControl; FindString : string ) : Integer; 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 TForm3.ListBox1DataObject( Control : TWinControl; Index : Integer; var DataObject : TObject ); begin DataObject := ObjList.Items[ index ]; end; end. object Form3: TForm3 Left = 0 Top = 0 Caption = 'Form3' ClientHeight = 189 ClientWidth = 780 Color = clBtnFace Font.Charset = GB2312_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Fixedsys' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 16 object lblNumber: TLabel Left = 16 Top = 144 Width = 72 Height = 16 Caption = 'lblNumber' end object lblColor: TLabel Left = 168 Top = 144 Width = 48 Height = 16 Caption = 'Label1' end object ListBox1: TListBox Left = 16 Top = 24 Width = 737 Height = 89 Style = lbVirtual TabOrder = 0 OnClick = ListBox1Click OnData = ListBox1Data OnDataFind = ListBox1DataFind OnDataObject = ListBox1DataObject end object Button1: TButton Left = 678 Top = 141 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 OnClick = Button1Click end object Edit1: TEdit Left = 312 Top = 141 Width = 121 Height = 24 TabOrder = 2 Text = 'Orange' end end TListBoxStyle specifies the way a listbox control gets its data and how it is drawn. TListBoxStyle = (
lbStandard,
lbOwnerDrawFixed,
lbOwnerDrawVariable,
lbVirtual,
lbVirtualOwnerDraw
);
The following table lists the values for the TListBoxStyle type:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论