none
Windows Forms Menu Strip Exit option show MessageBox twice RRS feed

  • Question

  • Hello,

    I have a menustrip which has an exit option on it.

    this displays a "are you sure" message box, and if yes is clicked, then the program closes. If not..well... it doesn't

    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
     if (MessageBox.Show("Are you sure you want to close", "Leave program? ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
      {
       System.IO.File.AppendAllText(errorlogpath, extmsg);
       this.Close();
       //Application.Exit();
       }
    }

    This worked fine.

    Then I added the same messagebox to the Formclosing option (ie "cross in the box")

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     {
      if (MessageBox.Show("Are you sure you want to close the program?", "Leave Program? ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
        {
          System.IO.File.AppendAllText(errorlogpath, extmsg);
          e.Cancel = false;
        }
        else
        {
        e.Cancel = true;
        }
    }

    and it seems now that this has affected the Exit menubar option.

    The formclosing works fine (ie: no duplicate message box if I press yes), but if I select the ext option from the menubar, I get the messagebox twice if I click yes.

    please, can you tell me what I am doing incorrectly?

    thanks

    Wednesday, February 21, 2018 8:09 AM

Answers

  • Remove the code from toolStripMenuItem1_Click, keeping this.Close() only.

    • Marked as answer by G-Oker Wednesday, February 21, 2018 9:17 AM
    Wednesday, February 21, 2018 8:40 AM

All replies