【问题标题】:Disable the Close Button until the user clicks on the LogOff MenuItem禁用关闭按钮,直到用户单击 LogOff MenuItem
【发布时间】:2010-11-30 17:18:29
【问题描述】:

我正在为图书馆管理系统创建一个应用程序。

我想最初禁用关闭按钮,并在用户单击菜单后启用它 项目注销。

有什么方法可以在我的应用程序中实现此功能?

我尝试在 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 之前关闭应用程序。

请帮助我完成这项任务。

提前致谢!

【问题讨论】:

  • 嗯,为什么不在人们关闭表单时将其注销?还是不够难?

标签: c#


【解决方案1】:

当 checkLogOff == false 时,你想取消事件,所以你应该设置 e.Cancel 为 True, 当 checkLogOff == true 时为 False。

【讨论】:

    【解决方案2】:

    我不会为此摆弄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");
        }
    }
    

    【讨论】:

    • 感谢您的帮助 现在一切正常!即使您的答案是正确的,但我只能将一个人的答案标记为正确答案,非常抱歉。无论如何,非常感谢您的及时帮助!
    【解决方案3】:

    你颠倒了 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; // <--
          }
      }
    

    【讨论】:

      【解决方案4】:

      改变这个:

      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;
      

      您没有取消表单加载。

      【讨论】:

        【解决方案5】:

        我知道这不是您寻求的答案,但如果他只想关闭应用程序,告诉用户转到另一个菜单项进行登录是很麻烦的。

        为什么告诉他他需要注销?你不能代表他调用注销函数,或者显示一个对话框询问他是否要注销?

        【讨论】:

        • 感谢您的建议我已将 LogOff 的编码包含在一个单独的方法中,当用户单击表单的关闭按钮时会自动调用它。非常感谢。
        【解决方案6】:

        我强烈建议您针对 e.CloseReason 进行测试,并且仅在用户关闭操作时执行您的取消逻辑,否则您的应用程序阻止系统关闭,并且“结束此操作”立即执行任务的对话框不会给您的用户留下好印象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-29
          • 1970-01-01
          • 2011-01-31
          • 1970-01-01
          相关资源
          最近更新 更多