开发者

Select WinForm ListView Item, Press Delete: Trigger Code

Let's say I have a list item populated with a few开发者_StackOverflow items, I select one and press delete.

I want something to happen when delete is pressed (and I'd like to know which item or items are selected). If this is possible, I'd like to know how to do this.

Thanks!


Set up your ListView to have an event handler for the KeyDown event. Then check that the key that was pressed was the delete key. Then use SelectedItems to see which items are selected and remove them. Make sure to go from the bottom up because your SelectedItems collection will be constantly changing.

private void listView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Delete)
        {
            for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
            {
                ListViewItem li = listView1.SelectedItems[i];
                listView1.Items.Remove(li);
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜