本文介绍了列表视图将selectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用的ListView ItemSelectionChanged 事件?我有一个的ListView ,这反过来又8列;和每行的第一列包含该行的编号,这是我后

How do I use the ListView's ItemSelectionChanged event? I have a ListView, which in turn has 8 columns; and the first column of each row contains the Id for the row and this is what I'm after.

我需要这个,因为标识是被用来从列表中选择信息。这份名单是填充的ListView ,但它并没有显示出所有的信息的。没有被显示的信息将被显示在的RichTextBox ,当然,它应该显示在的ListView所选项目。该ID是一样的,当然,但不管什么似乎我尝试,我不能达到这一数值。

I need this because the Id is to be used to select info from a list. This list is what populates the ListView, but it doesn't show all of the info. The info not being shown is to be displayed in a RichTextBox and, of course, it should display the info for that selected item in the ListView. The Id's are the same, of course, but no matter what I try I can't seem to reach that value.

我的code迄今:

foreach ([ClassName] x in [List])
{
    if (x.id == Convert.ToInt32(myListView.FocusedItem.Text))
    {
        rtbxSpecific.Text = x.name;
    }
}

这在 ListView中第一个对象的伟大工程,但不能用于下列任何地方抛出的。为什么会这样?这就像事件触发,但是它不知道有关在除了第一个列表中的任何其他项。

This works great for the first object in the ListView, but not for any of the following where it throws a NullReferenceException. Why does this happen? It's like the event fires, but it doesn't know about any other item in the list except for the first one.

我使用也试过 selectedItems [0]。文本 selectedItems [0] .SubItems [0]。文本,但它基本上是相同的故障。这实在是烦人的我,我会很感激,如果有人可以解释为什么它的工作原理是第 ListViewItem的,而不是下面的内容。

I've also tried using selectedItems[0].Text, selectedItems[0].SubItems[0].Text but it's basically the same fault. This is really annoying me and I would be really thankful if someone could explain why it works for the first ListViewItem, but not the following.

我甚至试过使用查找(...)的名单上:

I even tried using Find(...) on the list with:

[Class] cl = [List].Find(delegate([Class] q) {return q.id == Convert.ToInt32(myListView.FocusedItem.SubItems[0].Text);});

但没有骰子,这使我相信它的 FocusedItem ,自己目前正被讨厌,我不是这个在任何地方获得。在查找(...)方法是清洁剂的使用,我想,但现在我得到了同样的错误与两个。

But no dice, which leads me to believe its the FocusedItem that is being nasty right now and I'm not getting anywhere with this. The Find(...) method would be cleaner to use, I suppose, but right now I'm getting the same error with both.

推荐答案

简单的检查,看是否选定item.count是> 0

Simple check to see if the selected item.count was > 0.

if (myListView.SelectedItems.Count > 0)
{
    foreach ([Class] cl in [List])
    {
        if (myListView.SelectedItems[0].SubItems[0].Text == cl.[Field].ToString())
        {
            RichTextBox.Text = cl.[Field];
        }
    }
}

这篇关于列表视图将selectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:54