C# 中的 listView 和 .SelectedItem 帮助
在我的 winform(C#) 中,我有包含两列的 listView,用于显示搜索数组的结果,这是数组条目的基本详细信息(数组中的所有数据都是字符串类型)。我能够向列表视图添加和删除项目,但我希望能够获取列表视图中已单击的行的值。
我打算用它来更新显示完整条目信息,但我无法获得所选项目的值。我在其他地方阅读并看到人们提到 .SelectedIndex 属性,但当我尝试编码时,该属性对我不可用。
我可以使用两个列表框而不是列表视图,但列表视图更整洁。另外,我是一名软件设计(高中)学生,已经学习 C# 一年半了。我擅长编程,但当事情开始变得非常复杂时,我开始迷失方向。
有人可以帮忙吗?
Within my winform(C#) I have listView with two columns, which is used to display the results from searching an array, which is the basic details of an array entry(All data within the array is type string). I am able to add and remove items to the listview, but I would like to be able to gain the value of the row that has been clicked within the listview.
I am planning to use this to update the display full entry info, but I cannot gain the value of the selected item. I was reading elsewhere and saw people mentioning the .SelectedIndex property, but this property is not available to me when I am trying to code.
I could use two listboxes instead of a listview, but the listview is much neater. Also, I am a sofware design (high school) student, and have been learning C# for one and a half years. I am competent at programming, but I start to get lost when things start to get very complex.
Can anybody help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果 ListView 不处于 VirtualData 模式,则
ListView.SelectedItems
属性可以正常工作。The
ListView.SelectedItems
property works fine if the ListView is not in VirtualData mode.您应该使用 SelectedItem 属性和 Tag 属性
标签属性允许您设置任何类型,例如 MyClass
当您填充 ListView 时,设置 Tag 属性。
you should use the SelectedItem property and Tag property
tag property allow you to set any type for example MyClass
when you populate the ListView set the Tag property.
对于
ListView
检查 SelectedItemsFor
ListView
check SelectedItems您可以捕获
SelectedIndexChanged
事件,并且您可以这样做You could trap the
SelectedIndexChanged
event and in that you could do我已经解决了这个问题。在与我的软件课程中的一位朋友交谈后,他告诉我使用
listView1.FocusedItem.Index
,它非常适合我的需求,因为它获得了所选项目的索引。另外,感谢大家为我提供的帮助。I have fixed the problem. After talking to a friend in my software class, he told me to use
listView1.FocusedItem.Index
, which works perfectly for my needs, as it gains the index of the selected item. Also, thank you to everyone for their input in trying to help me.