例如,让它工作:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
this.Close();
return true;
}
else
{
return base.ProcessDialogKey(keyData);
}
}
虽然我正在禁用Alt + f4,但我无法使用e.Cancel = true;
,因为它会因关闭程序而禁用键H。
答案 0 :(得分:2)
试试这个:
解决方案1:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
this.Close();
return true;
}
else if (keyData == Keys.Alt | keyData == Keys.F4)
{
return base.ProcessDialogKey(keyData);
}
return true;
}
解决方案2:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Alt | e.KeyCode==Keys.F4)
{
e.Handled = true;
}
else if (e.KeyCode == Keys.H)
{
this.Close();
}
}
答案 1 :(得分:1)
试试这个:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
}
这样,只有当用户尝试通过用户界面关闭表单时才取消关闭。
答案 2 :(得分:1)
如何设置一些全局变量来关闭?
bool closeForm = false;
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.H)
{
closeForm = true;
this.Close();
return true;
}
else
{
return base.ProcessDialogKey(keyData);
}
}
并在您的FormClosing事件中,只需检查变量,如此
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!closeForm)
e.Cancel = true;
}