“列表索引越界”在 TListBox 上
我在表单上有一个 TListBox,并且使用 grp
添加项目
listbox1.ItemIndex := listbox1.Items.AddObject('msg', TObject(grp));
是一个整数。列表框设置为 lbOwnerDrawFixed。
在 onDrawItem
事件中,我在标记行上引发了异常 EStringListError
:
msg := (control as Tlistbox).Items.Strings[index]; // this line works
grp := integer((control as Tlistbox).Items.Objects[index]); // exception here
msg
和 grp
是本地字符串,整数变量。
项目 ### 引发异常类 EStringListError 并显示消息“列表索引超出范围 (1)”
I have a TListBox on a form, and items are added with
listbox1.ItemIndex := listbox1.Items.AddObject('msg', TObject(grp));
grp
is an integer. The listbox is set to lbOwnerDrawFixed
.
In the onDrawItem
event I get the exception EStringListError
raised on the marked line:
msg := (control as Tlistbox).Items.Strings[index]; // this line works
grp := integer((control as Tlistbox).Items.Objects[index]); // exception here
msg
and grp
are local string and integer variables.
Project ### raised exception class EStringListError with message 'List index out of bounds (1)'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
愚蠢的错误:我使用
grp := -1
作为默认组,AddObject
或Objects[index]
一定不喜欢它。Silly mistake: I was using
grp := -1
as the default group, whichAddObject
orObjects[index]
must not like.您只想存储一个整数,因此您应该将代码更改为
“无需在此处存储对象”,这使得整个事情变得更容易且更具可读性。
现在出现的异常是因为您无法使用索引检索对象,而必须使用与它们关联的字符串(
AddObject
的第一个参数)。正确的方法是这样的:另请参阅此 关于
AddObject< 的教程/代码>
。
You just want to store an integer, so you should change your code to
No need for storing objects here and this makes the whole thing much easier and more readable.
The exception you get now is because you can't retrieve objects using the index, but have to use the string you associated them with (the first parameter of
AddObject
). The correct way would be something like this:Also see this tutorial about
AddObject
.