如何从c#
中的列表视图中获取所选项目的行号private void listView1_DoubleClick(object sender, EventArgs e)
{
if( (listView1 row numer for selectitem) > 2 )
{
int indx = listView1.SelectedItems[0].Index;
listView1.Items[indx].Remove();
}
}
答案 0 :(得分:0)
即,改为侦听ItemSelectionChanged事件,并在事件数据上使用ItemIndex属性。
E.g。 Microsoft示例显示
private void ListView1_ItemSelectionChanged(
Object sender, ListViewItemSelectionChangedEventArgs e) {
var i = e.ItemIndex; // got the latest selection
}
答案 1 :(得分:0)
尝试使用SelectedIndexChanged事件...索引位置基于零,因此如果您有10个项目,则索引将为0 - 9.如果您认为0是第1行,则只需添加一个。最后,当未选择项目时,索引为-1
private void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListView1.SelectedIndex > -1)
{
// Add 1 so you have 1 - 10 instead of 0 - 9
int rowNumber = ListView1.SelectedIndex + 1;
// Your example says you want to delete the selected index
// so you still would want to use the selected index
ListView1.Items.RemoveAt(ListView1.SelectedIndex);
// After you remove the item, this method will fire again
// but the selected index will be -1 so none of this code will
// execute again.
}
}
请注意,此代码一次只支持选择一行。
希望这会有所帮助......