[DUG] Is it a bug in latest version of Delphi?
Karl Reynolds
kreynolds at chocfishsoft.co.nz
Tue Feb 16 11:16:12 NZDT 2010
One way to get around this would be to add your own descendant of
TListBox and add an Objects property that works as you would expect
(ie. use that instead of items.objects). Here's a quick sample I
knocked together:
>>
unit MyListBox;
interface
uses
SysUtils, Classes, Controls, StdCtrls, RTLConsts;
type
TMyListBox = class(TListBox)
private
function GetObject(AIndex: Integer): TObject;
procedure SetObject(AIndex: Integer; const AValue: TObject);
{ Private declarations }
protected
{ Protected declarations }
public
property Objects[AIndex: Integer]: TObject read GetObject write SetObject;
published
{ Published declarations }
end;
procedure Register;
implementation
type
TIPRStrings = class(TStrings);
function TMyListBox.GetObject(AIndex: Integer): TObject;
begin
Result := TObject(GetItemData(AIndex));
if GetLastError <> 0 then
TIPRStrings(Items).Error(SListIndexError, AIndex);
end;
procedure TMyListBox.SetObject(AIndex: Integer; const AValue: TObject);
begin
Items.Objects[AIndex] := AValue;
end;
procedure Register;
begin
RegisterComponents('Samples', [TMyListBox]);
end;
end.
<<
Alternatively, you could just use the relevant code yourself on your
own listbox where you need to
type
TIPRCustomListBox = class(TListBox);
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.AddObject('foo', TObject(-1));
i := Integer(TIPRCustomListBox(ListBox1).GetItemData(0));
if GetLastError <> 0 then // an error occurred, raise an exception or whatever
else ShowMessage(IntToStr(i));
end;
Cheers,
Karl
More information about the Delphi
mailing list