是这样的,我已经通过程序实现将某控件全屏显示。然后,我想通过按下“esc”键,使窗体还原。事先,已经将窗体的KeyPreview属性设置为true。代码:private void Form1_keypress(object sender, KeyPressEventArgs e)
{
if (Chr(e.KeyChar)==Keys.Escape.ToString())
{
splitContainer1.SplitterDistance = 220;
控件1.Height = splitContainer1.Panel2.Height;
menuStrip1.Show();
}
}
public static string Chr(int ascii)
{
if (ascii >= 1 && ascii <= 255)
{
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)ascii };
string str = asciiEncoding.GetString(byteArray);
return (str);
}
else
{
return "1";
}
}问题是,我按下键,该事件不响应。Visual Studio 2010窗体 响应键盘事件
{
if (Chr(e.KeyChar)==Keys.Escape.ToString())
{
splitContainer1.SplitterDistance = 220;
控件1.Height = splitContainer1.Panel2.Height;
menuStrip1.Show();
}
}
public static string Chr(int ascii)
{
if (ascii >= 1 && ascii <= 255)
{
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)ascii };
string str = asciiEncoding.GetString(byteArray);
return (str);
}
else
{
return "1";
}
}问题是,我按下键,该事件不响应。Visual Studio 2010窗体 响应键盘事件
直接e.Key==Keys.Escape判断吧
public static string Chr(int ascii)
{
if (ascii >= 1 && ascii <= 255)
{
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)ascii };
string str = asciiEncoding.GetString(byteArray);
return (str);
}
else
{
return "1";
}
}
主要是根本这个事件没有响应到。调试时,无法进入到该事件。
需要窗体激活
经验之谈,这种方法很简单好用,请给分
KeyDown可写如下进行判断
if (e.KeyCode == Keys.Escape)
{}
private void Form1_keypress(object sender, KeyPressEventArgs e)
{
弹框
}
我这样尝试了下,如果,我在初始化窗体时,将窗体中的其他控件的Enabled设置为false,按下esc键,可以进入到KeyPress事件中。然而,我不能在初始化将其他控件Enabled设置为false因此我在另一个方法中,由该方法触发得到其他控件Enabled为false,结果仍然无法进入到KeyPress事件中。
所以,我觉得原因就是,窗体没有得到焦点。但是我不知道如何得到。
捕获窗体或控件的键盘按下事件都是keydown事件,如果不知道根据英文翻译也能翻译下来啊
我这样尝试了下,如果,我在初始化窗体时,将窗体中的其他控件的Enabled设置为false,按下esc键,可以进入到KeyPress事件中。然而,我不能在初始化将其他控件Enabled设置为false因此我在另一个方法中,由该方法触发得到其他控件Enabled为false,结果仍然无法进入到KeyPress事件中。
所以,我觉得原因就是,窗体没有得到焦点。但是我不知道如何得到。焦点textbox 应该不会触发窗体keypress事件在textbox keypress事件 使用之前窗体的keypress事件即可
激活窗体能不能用this.Form.Activate()?