根据多少项调整 ListView 的大小?
如何根据 ListView 中的项目数调整 ListView 的高度?我试图获取被单击的项目的文本,但是每当用户单击没有项目的空间时,就会出现错误。 确切的错误是:
InvalidArgument=值“0”对于“索引”无效。 参数名称:索引。
我正在使用代码:
label14.Text = myListView1.SelectedItems[0].Text.ToString();
我认为删除项目下方的空间可以解决这个问题。谢谢!
How can I resize the height of a ListView depending on how many items are in that ListView? I'm trying to get the text of an item which is clicked, however whenever the user clicks on a space which has no item, there's an error.
The exact error is:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index.
I'm using the code:
label14.Text = myListView1.SelectedItems[0].Text.ToString();
I figured that removing the space below the items will solve this problem. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要修复错误,您应该检查用户是否实际单击了某个项目:
What you should do to fix your error is check to see whether the user actually clicked on an item:
每个项目的高度约为 5-7 像素,具体取决于所使用的字体/字体大小。所以你可以简单地做 myListView1.Height = myListView1.Items.Count * itemHeight;
要删除空列表项,您可以迭代并删除它们。
The height of each item is what, around 5-7 pixels depending on the font/font size used. So you can simply do myListView1.Height = myListView1.Items.Count * itemHeight;
To remove empty list items you can iterate through and remove them.
啊哈。我已经成功了。我用过:
这似乎有效。再次感谢您的帮助!
Aha. I've got it working. I used:
That seemed to do the trick. Thanks again for the help!