拦截键按删除

Ale*_*x F 3 c# listview winforms

我的用户组件中有一个列表视图.在listview属性LabelEdit为true.在listview上我有contextmenustrip项目删除快捷键Del.我如何能够按下按键Del如果编辑了一个单元格 - 删除单元格中的文本,如果不可编辑 - 删除Listview上的项目???

Mar*_*num 5

您可以通过绑定到KeyDown(或KeyUp)事件来简单地开始ListView:

listView1.KeyDown += listView1_KeyDown;
Run Code Online (Sandbox Code Playgroud)

然后在事件中:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)