【问题标题】:Intercept key press delete拦截按键删除
【发布时间】:2012-10-21 16:54:43
【问题描述】:

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

【问题讨论】:

  • What have you tired? 具体来说,您是否尝试过注册事件处理程序?另外,这是 Windows 窗体还是 WPF?

标签: c# winforms listview


【解决方案1】:

您可以从绑定到ListView 上的KeyDown(或KeyUp)事件开始:

listView1.KeyDown += listView1_KeyDown;

然后在事件中:

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多