Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept key press delete

I have a listview in my users component. On listview property LabelEdit is true. On listview i have contextmenustrip with item Delete with shortcut key Del. How can i catch key press Del that if a cell is edited - delete the text in the cell, if is not editable - delete Item on Listview???

like image 606
Alex F Avatar asked Dec 04 '25 17:12

Alex F


1 Answers

You could start simple with binding to the KeyDown (or KeyUp) event on the ListView:

listView1.KeyDown += listView1_KeyDown;

And then in the event:

void listView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete)
    {
        // Check if selected item is editable and act accordingly...

        // Bypass the control's default handling; 
        // otherwise, remove to pass the event to the default control handler.
        e.Handled = true;
    }
}
like image 185
Mario S Avatar answered Dec 06 '25 05:12

Mario S