禁用关闭按钮,直到用户单击LogOff MenuItem
我正在为图书馆管理系统创建应用程序。禁用关闭按钮,直到用户单击LogOff MenuItem
我想先关闭关闭按钮,并在用户点击菜单 项目注销后启用它。
有没有什么办法可以在我的应用程序中完成此功能?
我尝试在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*/
}
在执行应用程序,如果我不点击我收到的对话框中,但在注销菜单项后,我立刻按下OK应用程序关闭的消息框中的按钮。但我不想让用户在单击LogOff MenuItem之前关闭应用程序。
请帮我完成这个任务。
在此先感谢!
当checkLogOff == false时,您要取消该事件,因此您应该将e.Cancel设置为True, ,并在checkLogOff == true时将其设置为False。
感谢您的帮助现在它工作正常! – Sheetal 2009-09-23 11:08:28
为了达到这个目的,我不想摆弄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");
}
}
感谢您的帮助现在它工作正常! 即使你的回答是正确的,但我可以标记只有一个人的答案是正确的答案,所以很抱歉。无论如何非常感谢您的及时帮助! – Sheetal 2009-09-23 11:09:19
你倒e.Cancel
的设置它必须是:
e.Cancel = true;
当您需要阻止应用程序关闭。
==>
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 = true; // <--
this.ControlBox = false;
return;
}
else
{
this.ControlBox = true;
e.Cancel = false; // <--
}
}
更改此:
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;
你是不是取消形式负载。
我知道这不是你所寻求的答案,而是告诉用户去另一个菜单项登录,如果他只是想关闭应用程序是cubersome。
为什么告诉他他需要注销?不能只为他代表注销功能,或者显示一个对话框询问他是否要注销?
感谢您的建议我已经在单独的方法中包含了LogOff的编码,并在用户单击表单的关闭按钮时自动调用它。 非常感谢。 – Sheetal 2009-09-23 11:23:57
我强烈建议您对e.CloseReason进行测试,并且只执行取消逻辑,如果它是用户关闭操作,否则您的应用程序将将阻止系统关闭,并立即结束此任务对话框对用户来说不是一个好印象。
嗯,为什么不在关闭表单时将人员关闭?还是不够辛苦? – Will 2009-09-23 11:56:57