C# 从列表视图中选择索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11324471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:25:07  来源:igfitidea点击:

Select index from listview

c#

提问by 3D-kreativ

I'm having some problem to get the index of the selected row in a listview. I wonder why this code isn't working? I get a red line below the SelectedIndex

我在获取列表视图中所选行的索引时遇到了一些问题。我想知道为什么这段代码不起作用?我在 SelectedIndex 下方看到一条红线

    private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = lvRegAnimals.SelectedIndex;
        string specialData = motelManager.GetInfoFromList(index);
        UppdateSpecialData(specialData);
    }

Help is preciated. Thanks!

帮助是宝贵的。谢谢!

EDIT:

编辑:

For some strange reason I get two messages when I click on one of the lines in the listView!? First I get the previous number and then the number for the last clicked line. What could be wrong?

出于某种奇怪的原因,当我单击 listView 中的一行时,我收到两条消息!?首先我得到前一个数字,然后是最后点击行的数字。可能有什么问题?

 private void lvRegAnimals_SelectedIndexChanged(object sender, EventArgs e)
    {
        int index = lvRegAnimals.FocusedItem.Index;
        MessageBox.Show(Convert.ToString(index));
    }

It's working now when I added a check like this:

当我添加这样的支票时,它现在正在工作:

if(lvRegAnimals.SelectedIndices.Count > 0)

采纳答案by Habib

Because ListViewdoesn't contain any SelectedIndex, instead there is a property of SelectedIndices.

因为ListView不包含任何SelectedIndex,而是有一个属性SelectedIndices

var indices = lvRegAnimals.SelectedIndices;
//indices[0] you can use that to access the first selected index

ListView.SelectedIndices

ListView.SelectedIndices

When the MultiSelect property is set to true, this property returns a collection containing the indexes of all items that are selected in the ListView. For a single-selection ListView, this property returns a collection containing a single element containing the index of the only selected item in the ListView.

当 MultiSelect 属性设置为 true 时,此属性返回一个集合,其中包含在 ListView 中选择的所有项目的索引。对于单选 ListView,此属性返回一个包含单个元素的集合,该元素包含 ListView 中唯一选定项的索引。

回答by Habib

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Acquire SelectedItems reference.
        var selectedItems = listView1.SelectedItems;
        if (selectedItems.Count > 0)
        {
        // Display text of first item selected.
        this.Text = selectedItems[0].Text;
        }
        else
        {
        // Display default string.
        this.Text = "Empty";
        }
    }

回答by jaleel

Try :

尝试 :

listView1.FocusedItem.Index

This give you the index of the selected row.

这为您提供了所选行的索引。

回答by Roger Deep

There is another thread like this one, but here it goes again.

还有一个像这样的线程,但它又来了。

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

它可以返回NULL。SelectedIndexChanged 事件也可以触发两次。第一次,还没有选择任何东西。

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 具有正确的值...