有一个表 (DepID,DepName)
现在我 使用listbox读取所有的 Depname到listbox中,但是添加Depname到listbox的一个节点时候,我想同时保存depname的ID编号信息 到节点中。看了下说明使用Listbox.items.addobject这个方法可以..请高手写个示例看看,重点是类型释放如何处理

解决方案 »

  1.   


    添加
    ListBox1.Items.AddObject('sss',TObject(ID值))
    读取
    Integer(ListBox1.Items.Objects[索引])
      

  2.   

    也可以使用下面的方法:
    1.
    直接添加
    ListBox1.Items.Add(Depname+'|'+ID);
    读取
    ListBox1.Items.Strings[0];
    然后通过ExtractStrings将两个值分解
    var
      s:string;
      str:TStrings;
    begin
      str:=TStringList.Create;
      str.Clear;
      s:=self.ListBox3.Items.Strings[0];
      ExtractStrings(['|'],[],PChar(s),str);2.用连个ListBox,一个存放Depname,一个存放ID,通过Depname在ListBox1中的index,读取ListBox2中的ID值,当然这要求存放时要一一对应。
      

  3.   

    用两个ListBox
      

  4.   


    object是全局的,你这个方法没用。
      

  5.   

    var
      str: ^string;
      intI: Integer;
    begin
      //写入
      for intI:=0 to 10 do
      begin
        new(str);
        str^:= IntToStr(intI);
        self.ListBox1.Items.AddObject(IntToStr(IntI),TObject(str));
      end;
      //读取
      for intI:=0 to 10 do
      begin
        str:= Pointer(self.ListBox1.Items.Objects[intI]);
        showmessage(str^);
      end;
      //释放
      for intI:=self.ListBox1.Count-1 downto 0 do
      begin
        str:= Pointer(self.ListBox1.Items.Objects[intI]);
        dispose(str);
      end;
    end;
      

  6.   

    如果希望显示1个字符串,但是信息是1个字符串+1个整数,可以checklistbox1.items.addobject(str1,tobject(int1)); 
    如果希望信息是更多的内容,则需要分配一个结构来存放了,然后checklistbox1.items.addobject(str1,tobject(结构指针)); 删除items时需要释放对应的结构指针——如果borland当初能为每个checklistbox1.items[i]多存一个string属性而不是现在的pointer属性,就好(结构需要分配、释放,麻烦)了