C# 中的 listView 和 .SelectedItem 帮助

发布于 2024-11-15 08:34:54 字数 325 浏览 6 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄意 2024-11-22 08:34:54

如果 ListView 不处于 VirtualData 模式,则 ListView.SelectedItems 属性可以正常工作。

The ListView.SelectedItems property works fine if the ListView is not in VirtualData mode.

提笔书几行 2024-11-22 08:34:54

您应该使用 SelectedItem 属性和 Tag 属性

var item = (MyClass)  listView1.SelectedItems[0].Tag;

标签属性允许您设置任何类型,例如 MyClass
当您填充 ListView 时,设置 Tag 属性。

you should use the SelectedItem property and Tag property

var item = (MyClass)  listView1.SelectedItems[0].Tag;

tag property allow you to set any type for example MyClass
when you populate the ListView set the Tag property.

睫毛溺水了 2024-11-22 08:34:54

对于 ListView 检查 SelectedItems

For ListView check SelectedItems

怎樣才叫好 2024-11-22 08:34:54

您可以捕获 SelectedIndexChanged 事件,并且您可以这样做

ListView.SelectedListViewItemCollection listItems= 
            this.myListView.SelectedItems;

        foreach ( ListViewItem item in listItems)
        {
            MessageBox.Show(item.SubItems[0].Text);
            MessageBox.Show(item.SubItems[1].Text);
        }

You could trap the SelectedIndexChanged event and in that you could do

ListView.SelectedListViewItemCollection listItems= 
            this.myListView.SelectedItems;

        foreach ( ListViewItem item in listItems)
        {
            MessageBox.Show(item.SubItems[0].Text);
            MessageBox.Show(item.SubItems[1].Text);
        }
以酷 2024-11-22 08:34:54

我已经解决了这个问题。在与我的软件课程中的一位朋友交谈后,他告诉我使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文