我将项目添加到列表框:
for i:=0 to HomeWorkers.Count-1 do
begin
s := '['+HomeWorker[i].Id+'] ' + HomeWorker[i].Name;
Listbox1.Items.Add(s);
end;
现在我想获得所选项目的数量。 我可以得到它的标题:
ShowMessage(ListBox1.Items.Strings[Listbox1.ItemIndex]);
示例id`s:12880345,1274136。 我可以用
添加项目吗?'['+HomeWorker[i].Id+'] ' + HomeWorker[i].Name;
,但在ShowMessage中只显示HomeWorker [i] .Id没有字符串标题解析?提前致谢。 P.S我来自俄罗斯,很抱歉英语不好
答案 0 :(得分:5)
您可以将ID存储在Objects
属性中,只需将Integer
转换为TObject
:
ListBox1.Items.AddObject(Format('[%d] %s', [Homeworker.ID, Homeworker.Name]),
TObject(Homeworker.ID));
以后你可以检索它,回击:
ShowMessage(IntToStr(Integer(ListBox1.Items.Objects[ListBox1.ItemIndex])));
答案 1 :(得分:1)
据我所知,您希望从放入列表框的格式化文本中提取Id。如果我是对的,你可以这样做:
function GetIdFromListBoxText(const Text: string): string;
var
P1, P2: Integer;
begin
P1 := Pos('[', Text);
P2 := Pos(']', Text);
if (P1<>0) and (P2<>0) then
Result := Copy(Text, P1+1, P2-P1-1)
else
Result := '';
end;
此代码依赖于您的Id字符串不包含[
或]
的假设。
您可以在代码中使用它:
ShowMessage(GetIdFromListBoxText(ListBox1.Items[Listbox1.ItemIndex]));
答案 2 :(得分:0)
如果我做对了,也许这可能会回答他的问题?当他应该展示他的物体时,他正在他的例子中拉动物品。
试试这个:
showmessage(string(listbox1.items.object[listbox1.itemindex]));
取代:
ShowMessage(ListBox1.Items.Strings[Listbox1.ItemIndex]);