开发者

How do I add an item to a TListBox and associate it with a data object?

开发者 https://www.devze.com 2023-03-31 12:46 出处:网络
See the code: function ShowItem(MyData: TMyData): TListItem; var vListItem: TListItem; begin vListItem := ListView1.Items.Add;

See the code:

function ShowItem(MyData: TMyData): TListItem;
var
  vListItem: TListItem;
begin
  vListItem := ListView1.Items.Add;
  vListItem.Data := MyData;

  vListItem.ImageIndex := 1;开发者_运维百科
  vListItem.Caption := MyData.Caption;

  Result := vListItem;
end;

What do I need to do to get this to work for TListBox? ListView.Items property seems to be that of TListItem, where as ListBox.Items is not, which means the function will not be able to return the correct parameter for TListBox.

How may I convert this for TListBox?

Thanks


This should do:

function ShowItem(MyData: TMyData): Integer;
begin
  Result := ListBox1.Items.AddObject(MyData.Caption, MyData);
end;


There is no equivalent since the ImageIndex property for list views has no analogue for list boxes (unless you implement it yourself and custom draw).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号