简体   繁体   中英

Disable the Close Button until the user clicks on the LogOff MenuItem

I am creating an application for Library Management Systems.

I want to disable the close button initially and enable it once the user clicks on the menu item logoff.

Is there any way I could accomplish this functionality in my application?

I tried using the following code in the formClosing event but it is not working out.

  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;

  }

  }

The value of checkLogOff Variable is set as follows:

  public bool checkLogOff = false;

  private void logOffToolStripMenuItem_Click(object sender, EventArgs e)

  {

  checkLogOff = true;

  /*Code to update the logoff users in the database*/

  }

When executing the application if I dont click on the LogOff menu item I am getting the dialog box but immediately after I press the OK Button in the Message Box the Application closes. But I dont want to allow the user to close the application before clicking on the LogOff MenuItem.

Please help me out in accomplishing this task.

Thanks in advance!

I would not fiddle with the ControlBox property for this purpose, since it also removes the Maximize and Minimize buttons. The main issue with your code is that you should set Cancel to true in FormClosing to prevent the form from being closed. I think your code can be reduced to this, and still achieve what you want (given that we don't touch ControlBox ):

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = !checkLogOff;
    if (e.Cancel)
    {
        MessageBox.Show("Please Log Off before closing the Application");
    }
}

Change this:

MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = false;
this.ControlBox = false;
return;

To this:

MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = true;
this.ControlBox = false;
return;

You aren't canceling the form load.

当checkLogOff == false时,您想取消该事件,因此应将e.Cancel设置为True,而在checkLogOff == true时设置为False。

You inverted the setting of e.Cancel

it must be:

e.Cancel = true; 

when you need to prevent the application from closing.

==>

  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; // <--
      }
  }

I know that is not the answer you seek, but telling a user to go to another menue item to log of, if he just wants to close the application is cubersome.

Why tell him he needs to log off? Cant you just call the Logoff function on his behalf, or display a dialog that asks if he wants to log off?

我强烈建议您针对e.CloseReason进行测试,并且仅在用户关闭操作时才执行cancel-logic,否则您的应用程序阻止系统关闭,并且“立即结束此任务”对话框的印象不佳对您的用户。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM