本文整理汇总了C#中System.Windows.Forms.Control.PreviewKeyDown事件的典型用法代码示例。如果您正苦于以下问题:C# Control.PreviewKeyDown事件的具体用法?C# Control.PreviewKeyDown怎么用?C# Control.PreviewKeyDown使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。
在下文中一共展示了Control.PreviewKeyDown事件的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Form1
public Form1()
{
InitializeComponent();
// Form that has a button on it
button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
button1.KeyDown += new KeyEventHandler(button1_KeyDown);
button1.ContextMenuStrip = new ContextMenuStrip();
// Add items to ContextMenuStrip
button1.ContextMenuStrip.Items.Add("One");
button1.ContextMenuStrip.Items.Add("Two");
button1.ContextMenuStrip.Items.Add("Three");
}
// By default, KeyDown does not fire for the ARROW keys
void button1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
case Keys.Up:
if (button1.ContextMenuStrip != null)
{
button1.ContextMenuStrip.Show(button1,
new Point(0, button1.Height), ToolStripDropDownDirection.BelowRight);
}
break;
}
}
// PreviewKeyDown is where you preview the key.
// Do not put any logic here, instead use the
// KeyDown event after setting IsInputKey to true.
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}