110,534
社区成员
发帖
与我相关
我的任务
分享
private void Form1_Load(object sender, System.EventArgs e)
{
// Set these when the form loads:
// Have the form capture keyboard events first.
this.KeyPreview = true;
// Assign the event handler to the form.
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
// Assign the event handler to the text box.
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode.ToString() == "F")
{
// When the user presses both the 'Alt' key and 'F' key,
// KeyPreview is set to False, and a message appears.
// This message is only displayed when KeyPreview is set to True.
this.KeyPreview = false;
MessageBox.Show("KeyPreview is True, and this is from the FORM.");
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode.ToString() == "F")
{
// When the user presses both the 'Alt' key and 'F' key,
// KeyPreview is set to False, and a message appears.
// This message is only displayed when KeyPreview is set to False.
this.KeyPreview = true;
MessageBox.Show("KeyPreview is False, and this is from the CONTROL.");
}
}