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!
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:
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.
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).
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.
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With