我正在为图书馆管理系统创建一个应用程序。
我想先禁用close按钮,然后在用户单击菜单项logoff时启用它。
有没有办法在我的应用程序中实现这一功能?
我尝试在formClosing事件中使用以下代码,但它不起作用。
private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
if (checkLogOff == false)
{
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = false;
this.ControlBox = false;
return;
}
else
{
this.ControlBox = true;
e.Cancel = true;
}
}
checkLogOff变量的值设置如下:
public bool checkLogOff = false;
private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
{
checkLogOff = true;
/*Code to update the logoff users in the database*/
}
在执行应用程序时,如果我没有单击LogOff菜单项,我会看到对话框,但在我按下消息框中的OK按钮后,应用程序立即关闭。但我不想让用户在单击LogOff MenuItem之前关闭应用程序。
请帮我完成这项任务。
提前感谢!
发布于 2009-09-23 11:05:33
出于这个目的,我不会摆弄ControlBox
属性,因为它还删除了最大化和最小化按钮。代码的主要问题是,您应该在FormClosing
中将Cancel
设置为true
,以防止窗体被关闭。我认为你的代码可以简化到这一步,并且仍然可以实现你想要的(假设我们不接触ControlBox
):
private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !checkLogOff;
if (e.Cancel)
{
MessageBox.Show("Please Log Off before closing the Application");
}
}
发布于 2009-09-23 11:07:11
更改此设置:
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = false;
this.ControlBox = false;
return;
要这样做:
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = true;
this.ControlBox = false;
return;
您没有取消表单加载。
发布于 2009-09-23 11:05:16
当checkLogOff ==为false时,您希望取消事件,因此应将e.Cancel设置为True,并在checkLogOff ==为true时将其设置为False。
https://stackoverflow.com/questions/1465314
复制相似问题