请教ListView控件怎样可以一项赋两个值?
ListView1.Items.Add.Caption:='ABC';
在ListView1中添加一行的内容为“ABC”,希望给“ABC”赋一个值为'123',请问该怎样做?又是怎么才能调用到值'123'呢?
效果:在ListView1中显示为'ABC',但它对应的值为'123'
[解决办法]
类似于这样procedure TForm1.Button1Click(Sender: TObject);var li:TListItem; p:^string;begin li:=ListView1.Items.Add; new( p ); li.Caption:='ABC'; p^:='123'; li.Data:=p;end;procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);var p:^string;begin if Item.Data = nil then Exit; p:=Item.Data; showmessage( p^ );end;
[解决办法]
用一个指针结构存吧
PListData = ^TListData;
TListData = record
keyValue : string;
keyName : string;
....其它信息
end;
procedure TForm1.Button1Click(Sender: TObject);
var
li:TListItem;
p:PListData;
begin
li:=ListView1.Items.Add;
new( p );
p.keyValue := '123';
p.keyName := 'ABC' ;
li.Caption:=p.KeyName;
li.Data:=p;
end;
选择ListView;
if Lv.selected <> nil then
begin
showmessage(PListData(Lv.selected.Data).keyValue);
showmessage(PListData(Lv.selected.Data).keyName);
end;