获取ListView中选择的Item的索引

时间:2023-01-19 10:45:24

I've been searching for about an hour already and couldn't find a best solution. I am migrating from VB.NET to C# Forms and to C# WPF. Never mind that... so I use this code for C# forms and it works, but not in C# WPF

我已经搜索了大约一个小时,但找不到最佳解决方案。我正在从VB.NET迁移到C#Forms和C#WPF。没关系......所以我将此代码用于C#表单并且它可以工作,但不能在C#WPF中使用

 if (ListView1.SelectedItems.Count > 0)
            {
                for (lcount = 0; lcount <= ListView1.Items.Count - 1; lcount++)
                {
                    if (ListView1.Items[lcount].Selected == true)
                    {
                        var2 = lcount;
                        break;
                    }
                }
            }

this is the way I want to get the index of the item clicked in listbox. I have the error in .SELECTED

这是我想要获取列表框中单击项目的索引的方式。我在.SELECTED中有错误

please help.

请帮忙。

6 个解决方案

#1


14  

You can get SelectedIndex from listView. No need to traverse over all items because as per your code you seems to be interested in index of any selected item.

您可以从listView获取SelectedIndex。无需遍历所有项目,因为根据您的代码,您似乎对所选项目的索引感兴趣。

var2 = ListView1.SelectedIndex;

OR

要么

simply this will work if interested in only first index:

如果只对第一个索引感兴趣,这将有效:

if (lst.SelectedItems.Count > 0)
{
    var2 = lst.Items.IndexOf(lst.SelectedItems[0]);
}

#2


2  

For Visual Studio 2015, SelectedIndex does not seem to be available. Instead, you can use SelectedIndices[x] where x=0 will give you the first selected item:

对于Visual Studio 2015,SelectedIndex似乎不可用。相反,您可以使用SelectedIndices [x],其中x = 0将为您提供第一个选定项目:

listView.SelectedIndices[0]

You can also set the MultipleSelect property to false to only allow one item to be selected at a time.

您还可以将MultipleSelect属性设置为false,以便一次只允许选择一个项目。

#3


2  

If you are using the .NET Compact Framework, SelectedIndex is not supported. For a general solution, I prefer SelectedIndices:

如果您使用的是.NET Compact Framework,则不支持SelectedIndex。对于一般解决方案,我更喜欢SelectedIndices:

ListView.SelectedIndexCollection indices = lst.SelectedIndices;
if (indices.Count > 0)
{
    // Do something with indices[0]
}

#4


0  

Why don't bring back the SelectedIndex ? Add this extension after your current namespace.

为什么不带回SelectedIndex?在当前命名空间后添加此扩展。

public static class Extension
{
    public static int SelectedIndex(this ListView listView)
    {
        if (listView.SelectedIndices.Count > 0)
            return listView.SelectedIndices[0];
        else
            return 0;
    }
}

Encapsulate this class in a namespace called Extensions and then add this inside your projects namespace to use the extension.

将此类封装在名为Extensions的名称空间中,然后将其添加到项目名称空间中以使用该扩展名。

  using Extensions;

Then simply use like this

然后简单地使用这样

   private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectionindex = ListView1.SelectedIndex();
        ListViewItem seletedItem = ListView1.Items[selectionindex];           
    }

P.S. The extension method should have returned -1 on Else, but as long as you're using it from the SelectedIndexChanged event, you're fine as it won't get fired if there are no items. This is by design, as the SelectedIndexChanged event gets fired twice. Once to deselect the initial item, then to select the new one. Proper way is to return -1 and check for negative numer. This is also why someone here got and ArgumentOutOfRange Exception.

附:扩展方法应该在Else上返回-1,但只要您从SelectedIndexChanged事件中使用它,就可以了,因为如果没有项目,它将不会被触发。这是设计使然,因为SelectedIndexChanged事件被触发两次。一旦取消选择初始项目,然后选择新项目。正确的方法是返回-1并检查负数。这也是为什么有人和ArgumentOutOfRange异常的原因。

#5


0  

It can return NULL. Also the SelectedIndexChanged event can be FIRED TWICE. And the first time, there nothing selected yet.

它可以返回NULL。 SelectedIndexChanged事件也可以是FIRED TWICE。第一次,还没有选择。

So the only safe way to find it is like this:

所以找到它的唯一安全方法是这样的:

    private void lv1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lv1.FocusedItem == null) return;
        int p = lv1.FocusedItem.Index;

... now int p has the correct value...

...现在int p具有正确的值...

#6


0  

sColl.Clear(); string item = String.Empty;

sColl.Clear(); string item = String.Empty;

        if (listView1.SelectedItems.Count > 0)
        {
            for (int i = 0; i < listView1.SelectedItems.Count; i++)
            {
                if (listView1.SelectedItems[i].Selected)   
                {
                    int i2 = listView1.SelectedItems[i].Index;
                    item = listView1.Items[i2].Text;
                    sColl.Add(item);
                }
            }
        }

        listView1.SelectedItems.Clear();
        foreach (var itemS in sColl)
        {
            string items = itemS;
        }

        sColl.Clear();
        listView1.SelectedItems.Clear();

#1


14  

You can get SelectedIndex from listView. No need to traverse over all items because as per your code you seems to be interested in index of any selected item.

您可以从listView获取SelectedIndex。无需遍历所有项目,因为根据您的代码,您似乎对所选项目的索引感兴趣。

var2 = ListView1.SelectedIndex;

OR

要么

simply this will work if interested in only first index:

如果只对第一个索引感兴趣,这将有效:

if (lst.SelectedItems.Count > 0)
{
    var2 = lst.Items.IndexOf(lst.SelectedItems[0]);
}

#2


2  

For Visual Studio 2015, SelectedIndex does not seem to be available. Instead, you can use SelectedIndices[x] where x=0 will give you the first selected item:

对于Visual Studio 2015,SelectedIndex似乎不可用。相反,您可以使用SelectedIndices [x],其中x = 0将为您提供第一个选定项目:

listView.SelectedIndices[0]

You can also set the MultipleSelect property to false to only allow one item to be selected at a time.

您还可以将MultipleSelect属性设置为false,以便一次只允许选择一个项目。

#3


2  

If you are using the .NET Compact Framework, SelectedIndex is not supported. For a general solution, I prefer SelectedIndices:

如果您使用的是.NET Compact Framework,则不支持SelectedIndex。对于一般解决方案,我更喜欢SelectedIndices:

ListView.SelectedIndexCollection indices = lst.SelectedIndices;
if (indices.Count > 0)
{
    // Do something with indices[0]
}

#4


0  

Why don't bring back the SelectedIndex ? Add this extension after your current namespace.

为什么不带回SelectedIndex?在当前命名空间后添加此扩展。

public static class Extension
{
    public static int SelectedIndex(this ListView listView)
    {
        if (listView.SelectedIndices.Count > 0)
            return listView.SelectedIndices[0];
        else
            return 0;
    }
}

Encapsulate this class in a namespace called Extensions and then add this inside your projects namespace to use the extension.

将此类封装在名为Extensions的名称空间中,然后将其添加到项目名称空间中以使用该扩展名。

  using Extensions;

Then simply use like this

然后简单地使用这样

   private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectionindex = ListView1.SelectedIndex();
        ListViewItem seletedItem = ListView1.Items[selectionindex];           
    }

P.S. The extension method should have returned -1 on Else, but as long as you're using it from the SelectedIndexChanged event, you're fine as it won't get fired if there are no items. This is by design, as the SelectedIndexChanged event gets fired twice. Once to deselect the initial item, then to select the new one. Proper way is to return -1 and check for negative numer. This is also why someone here got and ArgumentOutOfRange Exception.

附:扩展方法应该在Else上返回-1,但只要您从SelectedIndexChanged事件中使用它,就可以了,因为如果没有项目,它将不会被触发。这是设计使然,因为SelectedIndexChanged事件被触发两次。一旦取消选择初始项目,然后选择新项目。正确的方法是返回-1并检查负数。这也是为什么有人和ArgumentOutOfRange异常的原因。

#5


0  

It can return NULL. Also the SelectedIndexChanged event can be FIRED TWICE. And the first time, there nothing selected yet.

它可以返回NULL。 SelectedIndexChanged事件也可以是FIRED TWICE。第一次,还没有选择。

So the only safe way to find it is like this:

所以找到它的唯一安全方法是这样的:

    private void lv1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lv1.FocusedItem == null) return;
        int p = lv1.FocusedItem.Index;

... now int p has the correct value...

...现在int p具有正确的值...

#6


0  

sColl.Clear(); string item = String.Empty;

sColl.Clear(); string item = String.Empty;

        if (listView1.SelectedItems.Count > 0)
        {
            for (int i = 0; i < listView1.SelectedItems.Count; i++)
            {
                if (listView1.SelectedItems[i].Selected)   
                {
                    int i2 = listView1.SelectedItems[i].Index;
                    item = listView1.Items[i2].Text;
                    sColl.Add(item);
                }
            }
        }

        listView1.SelectedItems.Clear();
        foreach (var itemS in sColl)
        {
            string items = itemS;
        }

        sColl.Clear();
        listView1.SelectedItems.Clear();