关闭窗体

来源:百度知道 编辑:UC知道 时间:2024/06/20 14:02:46
为了防止用户意外关闭窗体,需要在什么事件中编写代码,提示用户是否关闭窗体,而且事件中的代码应该如何写.

窗体的FormClosing事件

private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确定要关闭窗体吗?","退出确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question)!= DialogResult.Yes)
{
e.Cancel = true;//取消关闭
}
}

<script>
window.onbeforeunload = function()
{
if((event.clientX>document.body.clientWidth&&event.clientY<0)||event.altKey)
{
window.close();}
}
</script>

加段JS就差不多了