Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select WinForm ListView Item, Press Delete: Trigger Code

Tags:

c#

winforms

Let's say I have a list item populated with a few 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!

like image 305
sooprise Avatar asked Jun 23 '10 12:06

sooprise


People also ask

How to remove selected items from listview?

If you want to remove selected items, there are 2 processes for it. (a). Delete Listview Items through Command Button (b). Delete Listview Items through Press ‘ Delete ’ key Now follow it with details. (a). Delete ListView Items through Command Button:

Can I programmatically select an item in a Windows Forms listview?

Privacy policy. Thank you. This example demonstrates how to programmatically select an item in a Windows Forms ListView control. Selecting an item programmatically does not automatically change the focus to the ListView control. For this reason, you will typically also want to set the item as focused when selecting an item.

How to delete listview items in Salesforce form?

Delete Listview Items through Press ‘ Delete ’ key Now follow it with details. (a). Delete ListView Items through Command Button: Drag command button control to form and assign its Caption as ‘ Delete ’ and Name as ‘ Delete_Txt ’. And type following code in click event of this button. (b).

How to delete listview items in lvdata1?

Delete Listview Items through Press ‘Delete’ key: For remove selected item through ‘ Delete ’ key press from keyboard, just type code in LVData1 ’s ‘ KeyDown ’ event. The following code will explain easily.


1 Answers

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);
            }
        }
    }
like image 68
msergeant Avatar answered Nov 10 '22 01:11

msergeant