gpt4 book ai didi

delphi - 是否可以将记录存储在 ListBox 的 Item.Object 属性中?

转载 作者:行者123 更新时间:2023-12-03 15:20:32 25 4
gpt4 key购买 nike

我想为添加到列表框的每个项目存储一条记录。我需要将记录变成一个类来完成此任务吗?

TServerRec = record
ID: integer;
DisplayName: string;
Address: string;
Port: integer;
end;

procedure TMainForm.PopuplateServers;
var
server: TServerRec;
begin
for server in FServerList do
begin
lbServers.AddObject(server.DisplayName, server);
end;
end;

最佳答案

尝试像这样声明你的结构

type    
PServerRec = ^TServerRec;
TServerRec = record
ID: integer;
DisplayName: string[255];
Address: string[255];
Port: integer;
end;

//now filling your list of objects
procedure TForm1.Button1Click(Sender: TObject);
const
Max=10;
var
FServerList : Array [1..Max] of PServerRec;
server : PServerRec;
i : Integer;
begin
for i := 1 to Max do
begin
new(server);
server^.DisplayName:='Server '+IntToStr(i);
FServerList[i]:=server;
end;

for server in FServerList do
begin
lbServers.Items.AddObject(server.DisplayName, TObject(server));
end;
end;

//now to retrieve the info
procedure TForm1.lbServersDblClick(Sender: TObject);
var
server : TServerRec;
begin
server:=PServerRec(lbServers.Items.Objects[lbServers.ItemIndex])^;
ShowMessage(server.DisplayName);
end;

不要忘记调用dispose函数来释放每个分配的对象的内存。

关于delphi - 是否可以将记录存储在 ListBox 的 Item.Object 属性中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4191118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com