朝花朝拾

朝花昔时杯中酒

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Second example: same form with listview and two buttons:

// Will fail
procedure TForm1.AddString(Var S : String);
begin
with form1.listview1 do begin
if (Finddata(0,@s,true,true)=nil) then begin
with items.add do begin
data:=@s;
Caption:=s;
end;
end else
showmessage('Item already is in the listview');
end;
end;

// Working code
procedure TForm1.AddPChar(Var P : PChar);
begin
with form1.listview1 do begin
if (Finddata(0,P,true,true)=nil) then begin
with items.add do begin
data:=P;
Caption:=P;
end;
end else
showmessage('Item already is in the listview');
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var S : String;
begin
s := 'First string';
AddString(s);
s := 'Second string';
AddString(s);
s := 'Third string';
AddString(s);
end;

procedure TForm1.Button2Click(Sender: TObject);
Var P : PChar;
begin
P := 'First string';
AddPChar(P);
P := 'Second string';
AddPChar(P);
P := 'Third string';
AddPChar(P);
end;

 

posted on 2015-05-10 13:31  朝花朝拾  阅读(216)  评论(0编辑  收藏  举报