我想实现combobox.additem(string,TObject);
但是总是出现错误,要不就是乱码,不知道怎么给object分配内存!
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
ItemValue:string;
ItemObject:TObject;
begin
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add('select deptid,deptname from dept');
AdoQuery1.Open;
for i:=0 to AdoQuery1.RecordCount-1 do
begin
  ItemValue:=AdoQuery1.Fields.Fields[1].text;
//这里如果是  ItemValue:='item';就没有错误
  ItemObject:=TObject(ItemValue);
  ComboBox1.Items.AddObject(AdoQuery1.Fields.Fields[0].text,ItemObject);
  if not AdoQuery1.Eof then
        AdoQuery1.Next;
end;
end;procedure TForm1.ComboBox1Change(Sender: TObject);
begin
//这里出现的是乱码
  showmessage(string(combobox1.Items.Objects[combobox1.ItemIndex]));;
end;
不知道什么原因?

解决方案 »

  1.   

    procedure TFrm_HouseQZInfo.Combobox1DropDown(Sender: TObject);
    var
      s: string;
    begin
      combobox1.Clear;
      with DM.ADOQuery_Tmp do
      begin
        close;
        sql.clear;
        sql.add('select * from tablename');
        open;
        first;
        while not eof do
        begin
          s := fieldbyname('字段名称').value;
          Combobox1.Items.Add(s);
          next;
        end;
      end;
    end;
      

  2.   

    to  sailer_shi(笨笨虫)我想要用combobox.additem()方法,意图是显示一列,然后值我又是另外的。在web页面上:
    <select name="select1">
    <option value="string1">字符串1</option>
    <option value="string2">字符串2</option>
    <option value="string3">字符串3</option>
    </select>
    看到的内容是一个,然后保存的内容又是另外的值。
      

  3.   

    ComboBox1.Items.AddObject(AdoQuery1.Fields.Fields[0].text,ItemObject);
     把上面的改为下面的:
    ComboBox1.Items.AddObject(AdoQuery1.Fields.Fields[0].text,ItemObject(deptname ));
      

  4.   

    我来了
    如果要将int类型的数据放到object中可以直接这样
    AComboBox.Items.AddObject(ItemValue, TObject(iID));如果要将string类型的数据放到object中可以这样
    var
      sID: PString;
    begin
        New(sID);     //关键是这句
        sID^ := 'ID号';
        ComboBox1.Items.AddObject('aaa', TObject(sID^));
    end;
    显示showmessage(string(combobox1.Items.Objects[combobox1.ItemIndex]));如果要将一组数据放进去可以这样
    type
      PIntIDTreeInfo = ^TIntIDTreeInfo;
      TIntIDTreeInfo = record
        id: string;
        name: string;
        paremtid: string;
      end;
    var
      p: PIntIDTreeInfo;
    begin
            New(p);  //关键是这句
            p^.id := '1'
            p^.name := 'aaa';
            p^.parentid := '0';
            ComboBox1.Items.AddObject('aaa', p)
    end;
    showmessage(PIntIDTreeInfo(combobox1.Items.Objects[combobox1.ItemIndex].ID);   
      

  5.   

    ItemObject:TObject 要动态分配空间