开发者

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. Currently when you pres开发者_StackOverflow社区s 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. If the key pressed is an arrow key, set the Handled flag of the KeyPressEventArgs to true to prevent further processing.

A code example, based on an MSDN Forum post

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. Create a new class, derive it from listbox, then override the ProcessCmdKey.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜