显示的时候可以id(item),不像pb中的dw那么方便的。
^_^

解决方案 »

  1.   

    如果你是做数据库的,就用DBLOOKUPCOMBOBOX控件,如果不是,我的办法是,做个数组,根据ITEMINDEX来查找ID的值。
      

  2.   

    将id存入combobox的object当中:
    ComBobox1.Items.AddObject(item, TObject(id));这样就可以对应上了,
    ComBobox.Items.Objects[j]可以得到相应的值。
      

  3.   

    简单一些把ID加在ITEM之前一起显示,值取前面部分
      

  4.   

    liuting(游子)的好像可行,我也试试,呵呵……
      

  5.   

    自己重载控件
    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+
                    '&brvbar;'+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+
                    '&brvbar;'+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('&brvbar;',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('&brvbar;',str)+1,Length(str)-Pos('&brvbar;',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('&brvbar;',str)+1,Length(str)-Pos('&brvbar;',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('&brvbar;',str)+1,Length(str)-Pos('&brvbar;',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.
      

  6.   

    用不着重载,太麻烦了。用Item的AddObject,你可以把自己的数据放到每一个Item指向的数据链中去,要的时候再取出来。很方便的。
      

  7.   

    哇,值得学习,俺碰这问题时,只是简单的将近ID与ITEM一块显示出来,要ID时再取。
      

  8.   

    to:lianghu(山人) 
    和你一样的做法
      

  9.   

    简单方法,用两个combobox,有一个不显示,两个一一对应,分别存id and item
      

  10.   

    我试过liuting(游子)的了,是不是我写错了,下面为错误代码为:
    combobox1.Items.Addobject(fidrst.fields['item'],tobject(fidrst.fields['ID']));
    [error]invalid typecast
    strsel1:=combobox1.Items.Objects[combobox1.itemindex];
    [error]imcompatible type:'string' and 'tobject'
      

  11.   

    用一个数组存放ID,ID:array [0..100] of string ,取id时:
    id_val:=id[combobox.itemindex]
    不就行了吗?
      

  12.   

    index:=combobox.items.indexof(字符串),you know?
      

  13.   

    combobox1.Items.Addobject(fidrst.fields['item'],tobject(fidrst.fields['ID']));
    [error]invalid typecast
    strsel1:=combobox1.Items.Objects[combobox1.itemindex];
    [error]imcompatible type:'string' and 'tobject'
    那么我应怎么写上面那两句?
      

  14.   

    为什么不作一个record :两个域,一个ID、一个Name。
    把那些东西写道数组 of Record中,增加Items的时候,按数组的下标取Name,取出来的时候用itemindex 作数组下标取ID
      

  15.   

    rec = record
      id : string;
      name : string;
    end
    pRec = ^Rec;Write :
    var p:pRec;
    new(p);
    p^.id:="001";
    p^.name:="go";
    combobox1.items.addobject("hell0",Tobject(p))read:showmessage(pRec(combobox1.items.Objects[index])^.Fid);