Close Form Button Event

前端 未结 6 1989
暗喜
暗喜 2021-02-03 15:11

in my application, the user is first presented with the log in screen, and the form that shows up after you log in has a Menu Bar. On that menu bar are 2 items: \"log out\" and

6条回答
  •  日久生厌
    2021-02-03 15:44

    This should handle cases of clicking on [x] or ALT+F4

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       if (e.CloseReason == CloseReason.UserClosing)
       {
          DialogResult result = MessageBox.Show("Do you really want to exit?", "Dialog Title", MessageBoxButtons.YesNo);
          if (result == DialogResult.Yes)
          {
              Environment.Exit(0);
          }
          else 
          {
             e.Cancel = true;
          }
       }
       else
       {
          e.Cancel = true;
       }
    }   
    

提交回复
热议问题