s:='0'+Trim(inttostr(i));
和这个
s:='01';
不同吧!
  学过C语言吗?

解决方案 »

  1.   

    我用了没有什么不同啊,我觉得是不是你的字符串没有清空;
    在第二次使用s之前s := '';
    procedure TForm1.Button1Click(Sender: TObject);
    var
      s1, s2: string;
    begin
      s1 := '0' + IntToStr(1);
      s2 := '01';
      showmessage(format('%s %s',[s1,s2]));
      combobox1.Clear;
      combobox1.Items.AddObject(s1,TObject(s1));
      combobox1.Items.AddObject(s2,TObject(s2));
    end;
      

  2.   

    addobject的作用是在添加string的同时添加其对应的object
    你注释语句里要求显示的是Objects,而不是string,因此showmessage将与加入的s相关联的object强制转化为string后显示出来自然与单单显示string是不同的。
    将ShowMessage(string(ComboBox1.Items.Objects[i]));
    改为ShowMessage(string(ComboBox1.Items.Strings[i]));
    就没什么不同了
      

  3.   

    添加Objects时是需要自己分配内存的,你每次直接把s加入Objects很危险的,要么为作一个数组,要么用不同的变量
      

  4.   

    ShowMessage(string(ComboBox1.Items.Strings[i]));
    上面的这句话有什么意思?
    和这样有区别吗:ShowMessage(ComboBox1.Items.Strings[i]);
      

  5.   

    To (taber)
    改为ShowMessage(string(ComboBox1.Items.Strings[i]));
    和改为改为ShowMessage(ComboBox1.Items.Strings[i]);不是一样吗?
      

  6.   

    改成这样就可以了var
      I:integer;
      ss:TObject;
      s:string;
    begin
      ComboBox1.Items.Clear ;
      for i:=1 to 2 do
      begin
       s:='';
       s:=Format('0%d',[i]);
       ss:=TObject.Create ;
       ss:=TObject(s);
        ComboBox1.Items.AddObject(s, TObject(ss));
      end;
      

  7.   

    注意:  是Items.Strings[i],而不是string(Items.Objects[i])
        这当然不同,前者本身就是string,后者是与该string对应的object(这个object也许很复杂,比如一张图片等,例如你这里的object就是一个内容为s的object,这个object具备了对象的所有方法属性,具有内存空间的自管理,远远超出了一个string包含的内容)转化为string的内容
      

  8.   

    我只是说string一个string有什么必要。他的itemindex[0]就是'01',显示出的当然还是‘01’,但不是tobject的内容,你这样改只是结果好像对了,但要是TObject的内容是别的,你的结果就不对了
      

  9.   

    我的意思是 选择 ABC显示 01 选择DEF 显示 02
    但是换掉注释显示就不同。。
    请指导  谢谢
    procedure TForm1.Button1Click(Sender: TObject);
    var
      I:integer;
      s:string;
    begin
      for i:=1 to 2 do
      begin
        s:='0'+Trim(inttostr(i));
        if s='01' then
        ComboBox1.Items.AddObject('abc', TObject(s));
        if s='02' then
        ComboBox1.Items.AddObject('def', TObject(s));
      end;
    {
        s:='01';
      ComboBox1.Items.AddObject('abc', TObject(s));;
        s:='02';
      ComboBox1.Items.AddObject('def', TObject(s));
    }
    end;
    procedure TForm1.ComboBox1Click(Sender: TObject);
    var
       i:Integer;
       s:string;
    begin
        i:=ComBobox1.ItemIndex;
        s:=string(ComboBox1.Items.objects[i]);
        ShowMessage(s);
    end;
      

  10.   

    看来只有这样了:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TStore = class(TObject)
        StoreString: string;
      end;  TForm1 = class(TForm)
        Button1: TButton;
        ComboBox1: TComboBox;
        procedure ComboBox1Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.ComboBox1Click(Sender: TObject);
    var
      i: Integer;
      s: string;
    begin
      i := ComBobox1.ItemIndex;
      s := TStore(ComboBox1.Items.objects[i]).StoreString;
      ShowMessage(s);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      I: integer;
      s: string;
      st: TStore;
    begin
      {} for i := 1 to 2 do
      begin
        s := Format('0%d', [i]);
        st := TStore.Create;
        st.StoreString := s;
        if s = '01' then
          ComboBox1.Items.AddObject('abc', st);
        if s = '02' then
          ComboBox1.Items.AddObject('def', st);
      end;
    {
        s:='01';
      ComboBox1.Items.AddObject('abc', TObject(s));;
        s:='02';
      ComboBox1.Items.AddObject('def', TObject(s));
     }
    end;end.
      

  11.   

    procedure TForm1.ComboBox1Click(Sender: TObject);
    var
       i:Integer;
    begin
        i:=ComBobox1.ItemIndex;
        ShowMessage(string(ComboBox1.Items.Objects[i]));
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      I:integer;
      s:string;
    begin
      for i:=1 to 2 do
      begin
      S:='';//注意;就是这儿了
       s:='0'+Trim(inttostr(i));
        ComboBox1.Items.AddObject(s, TObject(s));
      end;{ 改变上面。。用下面注释语句。。结果就不同 
        s:='01';
      ComboBox1.Items.AddObject(s, TObject(s));
        s:='02';
      ComboBox1.Items.AddObject(s, TObject(s));   }
    end;