最终的解决方案:参照ahm的方法新建一个tstringlist的属性纪录建立的object,在destroy
中释放.不知是否有更好的方法
unit aComboBox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,DB;
type
TaComboBox = class(TComboBox)
private
FDataSet: TDataSet;
FDisplayText: string;
FIdText: string;
function GetIdValue: string;
{ Private declarations }
protected
FNeedFree:boolean;//ÐèÒªÔÚDestroryÖÐFree aObject
FObjectList:TStringList;//test now...
FBookmark:TBookmark;//¼Í¼DataSetµÄÖ¸Õë
FActiveState:boolean;//¼Í¼DataSetµÄ״̬
FDropDownEvent:TNotifyEvent;//±£´æDropDownEventµÄʼþ
procedure aDropDown(Sender:TObject);//×Ô¶¨ÒåDropDownEvent
procedure GetList;//»ñÈ¡Áбí
procedure FreeAObject;//ÊÍ·ÅAObject;
{ Protected declarations }
public
procedure aRefresh;
property IdValue:string read GetIdValue;//»ñÈ¡IdµÄÖµ
constructor Create(AOwner:TComponent);override;
destructor Destroy; override;
{ Public declarations }
published
property DataSet:TDataSet read FDataSet write FDataSet;//±í
property DisplayText:string read FDisplayText write FDisplayText ;//ÏÔʾµÄ×Ö¶Î
property IdText:string read FIdText write FIdText;//¹Ø¼ü×Ö
{ Published declarations }
end;
TaComboBoxIdObject=class
aGuid:string;
// destructor destroy;override;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Autumn', [TaComboBox]);
end;
{ TaComboBox }
procedure TaComboBox.aDropDown(Sender: TObject);
begin
try
{ inherited DropDown }
if Assigned(FDropDownEvent) then FDropDownEvent(Sender);
finally
if Items.Count=0 then GetList;
end;
end;
procedure TaComboBox.aRefresh;
begin
FreeAObject;
FObjectList.Clear;
FNeedFree:=false;
Items.Clear;
end;
constructor TaComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FObjectList:=TStringList.Create;
FDropDownEvent:=OnDropDown;
OnDropDown:=aDropDown;
end;
destructor TaComboBox.Destroy;
begin
if FNeedFree then FreeAObject;
FObjectList.Free;
inherited;
end;
procedure TaComboBox.FreeAObject;
var aObject:TaComboBoxIdObject;
i:integer;
begin
for i:=0 to FObjectList.Count-1 do begin
if Assigned(FObjectList.Objects) then begin
aObject:=TaComboBoxIdObject(FObjectList.Objects);
aObject.Free;
end;
end;
end;
function TaComboBox.GetIdValue: string;
begin
result:='';
if Items.Count=0 then Exit;
if Items.Objects[ItemIndex] is TaComboBoxIdObject then begin
result:=TaComboBoxIdObject(Items.Objects[ItemIndex]).aGuid;
end
else
result:=IntToStr(Integer(Items.Objects[ItemIndex]));
end;
procedure TaComboBox.GetList;
var aObject:TaComboBoxIdObject;
begin
// Items.Clear;
with FDataSet do
try
FActiveState:=Active;
if Active then begin
FBookmark:=GetBookmark;
DisableControls;
end
else
Open;
First;
FNeedFree:=FieldByName(FIdText).DataType=ftGuid;
while Not FDataSet.Eof do begin
if FNeedFree then begin
FNeedFree:=true;
aObject:=TaComboBoxIdObject.Create;
aObject.aGuid:=FieldbyName(FIdText).AsString;
FObjectList.AddObject(FieldByName(FDisplayText).AsString,aObject);
// Items.AddObject(FieldByName(FDisplayText).AsString,aObject);
end
else begin
Items.AddObject(FieldByName(FDisplayText).AsString,
TObject(FieldByName(FIdText).AsInteger) );
end;
Next;
end;
if FNeedFree then
Items.Assign(FObjectList);
finally
FDataSet.Active:=FActiveState;
if FDataSet.Active then begin
FDataSet.GotoBookmark(FBookmark);
FDataSet.EnableControls;
FDataSet.FreeBookmark(FBookmark);
end;
end;
end;
{ TaComboBoxIdObject }
{ TaComboBoxIdObject }
{destructor TaComboBoxIdObject.destroy;
var a:tstringlist;
begin
a:=tstringlist.create;
if fileexists('d:/a.txt') then
a.LoadFromFile('d:/a.txt');
a.add(' application.MessageBox(a,b,0);'+timetostr(now));
a.savetofile('d:/a.txt');
a.free;
inherited;
end;}
end.