combox 的AddObject

                            我從數據庫中選出兩個字段:
 id  name
  1   AAA
  2   BBB
  3   CCC

將name字段寫到combox 的items中,我還想保留 id的值,
以便用戶選擇了 AAA 后我可以將 1  寫囘數據庫,
combobox用那個屬性來保存 id 值呢?

能不能實現這種功能? 
*********************************************************************
type
  PIDName = ^TIDName;
  TIDName = record
    ID, Name: string;
  end;


load:
procedure LoadData(const ID, Name: string; List: TStrings);
var
  Data: PData;
begin
  New(Data);
  Data^.ID := AID;
  Data^.Name := AName;
  List.AddObject(AName, Pointer(Data));
end;


  Combobox1.Items.BeginUpdate;
  while not ADOQuery1.Eof do
  begin
    LoadData(ADOQuery1.Fields[0].AsString, ADOQuery1.Fields[1].AsString, Combobox1.Items);
    ADOQuery.Next;
  end;
  Combobox1.Items.EndUpdate;


使用:

var
  Data: PData;
begin
  Data := PData(Combobox1.Items.Objects[Combobox1.ItemIndex]); 
  if not Assigned(Data) then
    raise Exception.Create('Error');
  Data....
end;

free:
  for I := Combobox1.Items.Count - 1 downto 0 do
    FreeMem(Pointer(Combobox1.Items.Objects[i]));
                                

查看回复