我在a上添加了一个keyPress事件ListView.通过我的事件断点,我可以看到大多数键触发事件.然而,其中一些,我感兴趣的(删除),只是不会触发我的事件.
这有点奇怪吗?不,我的键盘上没有坏掉的按键:D
private void listView1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Delete)
{
ListView target = (ListView)sender;
if (target.SelectedIndices != null && target.SelectedIndices.Count > 0)
{
string ric = target.SelectedItems[0].SubItems[0].Text;
//target.Items.RemoveAt(target.SelectedIndices[0]);
ListModels.getInstance().getModel("Vols").removeRic(ric);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Bra*_*ley 40
原因是KeyPress事件根据您按下的字符键向控件发送一个字符.但是,正如您所期望的那样,删除键不代表字符,因此是非字符键.
因此,使用Keypress事件将不会发生任何事情,因为您已经注意到了.您应该使用KeyDown或KeyUp事件,其中任何一个都可以正常工作.细微之处在于您是否希望在按下或放开钥匙时触发事件.
在 KeyDown 中使用如下条件,
if (e.KeyCode == Keys.Delete)
{
// Your Logic....
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39737 次 |
| 最近记录: |