To (taber) 改为ShowMessage(string(ComboBox1.Items.Strings[i])); 和改为改为ShowMessage(ComboBox1.Items.Strings[i]);不是一样吗?
改成这样就可以了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;
我的意思是 选择 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;
看来只有这样了: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.
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;
在第二次使用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;
你注释语句里要求显示的是Objects,而不是string,因此showmessage将与加入的s相关联的object强制转化为string后显示出来自然与单单显示string是不同的。
将ShowMessage(string(ComboBox1.Items.Objects[i]));
改为ShowMessage(string(ComboBox1.Items.Strings[i]));
就没什么不同了
上面的这句话有什么意思?
和这样有区别吗:ShowMessage(ComboBox1.Items.Strings[i]);
改为ShowMessage(string(ComboBox1.Items.Strings[i]));
和改为改为ShowMessage(ComboBox1.Items.Strings[i]);不是一样吗?
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;
这当然不同,前者本身就是string,后者是与该string对应的object(这个object也许很复杂,比如一张图片等,例如你这里的object就是一个内容为s的object,这个object具备了对象的所有方法属性,具有内存空间的自管理,远远超出了一个string包含的内容)转化为string的内容
但是换掉注释显示就不同。。
请指导 谢谢
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;
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.
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;