Close Form Button Event

前端 未结 6 1987
暗喜
暗喜 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:33

    Try this:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // You may decide to prompt to user else just kill.
        Process.GetCurrentProcess().Goose();
    } 
    
    0 讨论(0)
  • 2021-02-03 15:35
    private void mainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
            System.Windows.Forms.Application.Exit();
        }
        else
        {
            this.Activate();
        }  
    }
    
    0 讨论(0)
  • 2021-02-03 15:35

    Apply the below code where you want to make code to exit application.

    System.Windows.Forms.Application.Exit( )

    0 讨论(0)
  • 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;
       }
    }   
    
    0 讨论(0)
  • 2021-02-03 15:47

    If am not wrong

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
       //You may decide to prompt to user
       //else just kill
       Process.GetCurrentProcess().Kill();
    } 
    
    0 讨论(0)
  • 2021-02-03 15:56

    Try This: Application.ExitThread();

    0 讨论(0)
提交回复
热议问题