[英]C# prevent default action on a listbox control when the right or left arrow keys are pressed
I have a listbox control in a windows app and I want to disable the default right and left arrow keydown event triggers.我在 windows 应用程序中有一个列表框控件,我想禁用默认的左右箭头 keydown 事件触发器。 Currently when you press right or left arrows the selected item travels up and down the listbox.当前,当您按向右或向左箭头时,所选项目会在列表框上下移动。 I want to add my own actions.我想添加我自己的操作。
Try adding an event handler to the ListBox.KeyDown
event.尝试将事件处理程序添加到ListBox.KeyDown
事件。 If the key pressed is an arrow key, set the Handled
flag of the KeyPressEventArgs
to true
to prevent further processing.如果按下的键是箭头键,请将KeyPressEventArgs
的Handled
标志设置为true
以防止进一步处理。
A code example, based on an MSDN Forum post基于MSDN 论坛帖子的代码示例
private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
If (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
e.Handled = true;
}
You have to override the ProcessCmdKey method in the listbox control.您必须重写列表框控件中的ProcessCmdKey方法。 Create a new class, derive it from listbox, then override the ProcessCmdKey.创建一个新的 class,从列表框派生它,然后覆盖 ProcessCmdKey。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.