Get single listView SelectedItem

后端 未结 9 640
闹比i
闹比i 2020-12-01 12:39

I have the MultiSelect property of the listView set to false and I\'m trying to get a single listViewItem. But the available property is SelectedItems

9条回答
  •  無奈伤痛
    2020-12-01 13:24

    Sometimes using only the line below throws me an Exception,

    String text = listView1.SelectedItems[0].Text; 
    

    so I use this code below:

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if (listView1.SelectedIndices.Count <= 0) 
        { 
            return; 
        } 
        int intselectedindex = listView1.SelectedIndices[0]; 
        if (intselectedindex >= 0) 
        {
            String text = listView1.Items[intselectedindex].Text;
    
            //do something
            //MessageBox.Show(listView1.Items[intselectedindex].Text); 
        } 
    }
    

提交回复
热议问题