自己重载控件
unit myDBCombo;
 interfaceuses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,DB;type
  TcolorDBCombo = class(TComboBox)
  private
    { Private declarations }
    { Private declarations }
    FSeparator:string;
    FCurrentID:string;
 //   FCurrentData:string;
    Fstringlist:TstringList;     //在STRINGLIST中包含两部分,前部分是AINDEXFIELD,后面
    FSpaceText:string;                             //部分是ADATAFIELD,中间用|隔开
    FAddtionalData:Boolean;
    bShowSpace:Boolean;  
    bShowID:Boolean;       function GetCurrentID:string;
    function GetCurrentData:string;
    function GetIndexData(const IndexID:integer):string;
    procedure SetKeyValue(sVal:string);
  protected    { Protected declarations }
  public
    { Public declarations }
     property ItemIndex;
     property Datas[const IndexID:integer]:string read GetindexData;
     procedure FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string);
  published
    { Published declarations }
    property style;
    property Items;
    property Separaror:string read Fseparator write Fseparator;
    property ShoeID:Boolean read bShowID write bShowID;
    property ShowSpace:Boolean read bShowSpace write bShowSpace;
    property AddtionalData:Boolean read FAddtionalData write FAddtionalData;
    property SpaceText:string read FSpaceText write FSpaceText;
    property CurrentID:string read GetCurrentID;
    property CurrentData:string read GetCurrentData;
    property KeyValue:string  write SetKeyValue;
    property OnKeyDown;
constructor create(AOwner :TComponent);override;
destructor destroy;override;
  end;procedure Register;implementationprocedure Register;
begin
  RegisterComponents('Standard', [TcolorDBCombo]);
end;constructor TcolorDBCombo.create(AOwner:Tcomponent);
begin
  inherited;
  Fseparator:='';
  FCurrentID:='';
  bShowSpace:=false;
  bShowID:=true;
  FAddtionalData:=False;
  Self.style:=csDropDownList ;
  FstringList:=TstringList.Create;
end;destructor TcolorDBCombo.destroy;
begin
  FstringList.Free;
  inherited;
end;procedure TcolorDBCombo.FillItems(ADataset:TDataset;AShowField:string;AIndexField:string;ADataField:string);
var ABookMark:TBookMark;begin
    ABookMark:=ADataSet.GetBookMark;
    Self.Items.clear;
    FstringList.clear;
    if ADataSet.Active then
    begin      if bShowSpace then
       begin
       if FAddtionalData then
       begin
       Self.Items.Add(FSpaceText);
       FstringList.Add(''+'|'+'');
       end
       else
       begin
       Self.Items.Add(FSpaceText);
       FstringList.Add('');
       end;
       end;
      try
       ADataSet.First;
       while not ADataSet.Eof do
         begin
         if AIndexField<>'' then
         begin
         if not bShowID then
           begin
            Self.Items.Add(ADataSet.FieldByName(AShowField).AsString);
            if FAddtionalData then
            FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+
                '|'+ADataSet.FieldByName(ADataField).AsString)
            else
            FstringList.Add(ADataSet.FieldByName(AIndexField).AsString);
           end
         else
           begin
             Self.Items.Add(ADataSet.FieldByName(AIndexField).AsString +
                FSeparator + ADataSet.FieldByName(AShowField).AsString);
             if FAddtionalData then
             FstringList.Add(ADataSet.FieldByName(AIndexField).AsString+
                '|'+ADataSet.FieldByName(ADataField).AsString)
             else
             FstringList.Add(ADataSet.FieldByName(AIndexField).AsString);
           end;
          end
          else
            Self.Items.Add(ADataSet.FieldByName(AShowField).AsString);
          ADataSet.Next;
          end;
      except
MessageBox(Handle,'填写下拉框错误,请检查字段是否存在!','错误',MB_ICONERROR);
  end;//try..except
      ADataSet.GotoBookMark(ABookMark);
      ADataSet.FreeBook(ABookMark);
      end
      else
MessageBox(Handle,Pchar(ADataSet.Name+'没有打开'),'错误',MB_ICONERROR);
    Self.ItemIndex:=0;//Display First Item
end;function TcolorDBCombo.GetCurrentID:string;
var str:string;
begin
 if ItemIndex <> -1 then
begin
    if FAddtionalData then
    begin
    str:= FStringList.Strings[ItemIndex];
    Result:=copy(str,1,Pos('|',str)-1);
    end
    else
    Result:=FStringList.Strings[ItemIndex];
  end
else
Result := '';
end;function TcolorDBCombo.GetCurrentData:string;
var str:string;
begin
  if ItemIndex<>-1 then
    begin
      if FAddtionalData then
      begin
      str:= FStringList.Strings[ItemIndex];
      Result:=copy(str,Pos('|',str)+1,Length(str)-Pos('|',str));
      end
      else
      Result:=FStringList.Strings[ItemIndex];
    end
  else
     Result:='';
end;function TcolorDBCombo.GetIndexData(const IndexID:integer):string;
var str:string;
begin
    if FAddtionalData and (IndexID<>-1) then
    begin
    str:=FStringList.Strings[IndexID];
    Result:=copy(str,Pos('|',str)+1,Length(str)-Pos('|',str));
    end
    else
    result:='';
end;procedure TcolorDBCombo.SetKeyValue(sVal: string);
var Index:Integer;
    str:string;
begin
   if sVal='' then exit;
   for Index:=0 to FStringList.Count-1 do begin
      if FAddtionalData  then  begin
        str:=FStringList.Strings[Index];
        str:=copy(str,Pos('|',str)+1,Length(str)-Pos('|',str));
        if str=sVal then begin
           self.ItemIndex:=Index;
           Exit;
        end;
      end else begin
        str:=FStringList.Strings[Index];
        if str=sVal then begin
           self.ItemIndex:=Index;
           Exit;
        end;
      end;   end;end;end.