定义一个全局变量FList:TStringList;
procedure TForm1.FormCreate(Sender: TObject);
begin
  ADOQuery1.Open;
  FList:=TStringList.Create;
  FList.Clear;
  with adoquery1 do
  begin
    First;
    while not Eof do
    begin
      Flist.AddObject(FieldByName('FName').AsString,TObject(FieldByName('FNumber').AsString));//是这样吗?
      Next;
    end;
  end;
end;上面加了对象之后
要如何读取呢?我是如下读取的,但是不正确
procedure TForm1.Button1Click(Sender: TObject);
var
  i:Integer;
  s:string;
begin
  for i:=0 to FList.Count-1 do
  begin
   s:=string(Flist.Objects[i]);
   ShowMessage(s);
  end;
end;还有关于释放的问题,要不要先释放FList里面的对象??
procedure TForm1.FormDestroy(Sender: TObject);
var
  i:Integer;
begin
  for i:=0 to Flist.Count-1 do//需要这样吗?
    FList.Objects[i].Free;
  FList.Free;
end;

解决方案 »

  1.   

    TStringList这个是个抽像类啊,你在构造时,应该为FList:=TList.Create;
    好像这样就可以了,不知对不.
      

  2.   

    意思是不是字符A--字符B
    方法有很多:
    1.用两面个stringlist
    2.用一个stringlist用分隔符表示两个字符,如aaaaa&bbbb 使用的时候用stringreplace()函数分开。
    3.使用addobject('str',..)Note: The TStringList object does not own the objects you add this way. Objects added to the TStringList object still exist even if the TStringList instance is destroyed. They must be explicitly destroyed by the application.
    Note: For sorted lists, AddObject raises an EListError exception if the string S already appears in the list and Duplicates is set to dupError. If Duplicates is set to dupIgnore, trying to add a duplicate string causes AddObject to return the index of the existing entry.
    从上面的帮助可以知道它保存的对象必需自己释放.
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure testclick(sender: tobject);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      s: tstringlist;
      b: Tbutton;
      str: string;
    begin
      s := TstringList.Create;
      try
        b := Tbutton.create(self);
        s.AddObject('hello', b);
        Tbutton(s.Objects[0]).Parent := self;
        Tbutton(s.Objects[0]).onclick := testclick;
        //b.free; // 对象要自己释放,未释放前还可以发生事件。
        str := 'go go go ';
        s.addobject('hello world', tobject(str));
        text := string(s.Objects[1]); // 如果是string则应该由系统释放,但我不确定这个。
      finally
        s.free;
      end;
    end;procedure TForm1.testclick(sender: tobject);
    begin
      showmessage('click');
    end;end.
      

  3.   

    hsgrass37301(零点) 
    string(s.Objects[1]); 这样子不是和我的一样吗?不行啊
      

  4.   

    应该可以的,
    你试下分开这句看一下有没有问题
        Flist.AddObject(FieldByName('FName').AsString,TObject(FieldByName('FNumber').AsString));//是这样吗?s := FieldByName('FNumber').AsString;
    Flist.AddObject(FieldByName('FName').AsString,TObject(s));//是这样吗?
    showmessage(string(Flist.objects[i]));
      

  5.   

    建议使用其它类型,或方法,因为string是动态分配的,
    AddObject(const S: string; AObject: TObject)里面执行:procedure TStringList.InsertItem(Index: Integer; const S: string; AObject: TObject);
    begin
      Changing;
      if FCount = FCapacity then Grow; // Grow里面是重新分配内容给一个记录型Flist^[index]变量
      if Index < FCount then
        System.Move(FList^[Index], FList^[Index + 1],
          (FCount - Index) * SizeOf(TStringItem));
      with FList^[Index] do
      begin
        Pointer(FString) := nil;
        FObject := AObject; // 这里保存外部object的门牌.这样只会将使Fobject指向Aobject的位置,
        FString := S;
      end;
      Inc(FCount);
      Changed;
    end;现在使用是addobject('aaa', Tobject(string));
    string是由系统管理的,在一个过程内使用完就释放,
    但为什么有时可以正常访问,这个是我能力外的问题。使用record其它方法会好一点,我认为。
      

  6.   

    应该先定义一个类,比如字符串类
    然后在addobject(s,类)
      

  7.   

    哥呃,s:=string(Flist[i]);这样不行吗?
    FList.Free;就这样有什么错吗?