2010-03-31 47 views
1

我想实现空间的一些自定义行为ListView。基本上我想切换光标下的项目选择的状态 - 这应该是相当简单的覆盖.net中的空格键的默认行为WinForms ListView

this.FocusedItem.Selected = !this.FocusedItem.Selected; 

但很可惜,它也做了默认的动作,这是选择关注项目。通过这种方式,我无法“取消”选中重点项目。我查找过类似的问题,他们建议使用PreviewKeyDown事件,其中我将处理该键并禁止ListView执行其默认操作。但是,PreviewKeyDown事件参数没有“处理”属性,所以我不能'吃'这个键。

回答

1

这个工作,你想:

private void listView1_KeyDown(object sender, KeyEventArgs e) { 
    if (e.KeyData == Keys.Space) { 
    listView1.FocusedItem.Selected = !listView1.FocusedItem.Selected; 
    e.Handled = e.SuppressKeyPress = true; 
    } 
} 
+0

这个作品,谢谢。根据MSDN - http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress.aspx - 设置SuppressKeyPress也设置处理,所以只有“e.SuppressKeyPress = true”是必要的。 – Axarydax 2010-03-31 10:28:54