开发者

Form Closing help

How can I call the button1_Click event in the form closing event so I don't have to copy and paste the code from button1_Click?

      public void button1_Click(object sender, EventArgs e)
   {
       //Yes or no message box to exit t开发者_开发技巧he application
       DialogResult Response;
       Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
       if (Response == DialogResult.Yes)

           // Exits the application
           Application.Exit();
   }

   public void xGameThemeComboBox_SelectedIndexChanged(object sender, EventArgs e)
   {
       string folder = Application.StartupPath;
       string theme = (string)xGameThemeComboBox.Items[xGameThemeComboBox.SelectedIndex];
       string path = System.IO.Path.Combine(folder, theme + ".jpg");
       Image newImage = new Bitmap(path);
       if (this.BackgroundImage != null) this.BackgroundImage.Dispose();
       {
           this.BackgroundImage = newImage;
       }

   }

   private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
   {
      // call button1_Click here 
   }


what you really need to do:

      private bool ShowClose()
   {
       //Yes or no message box to exit the application
       DialogResult Response;
       Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
       return Response == DialogResult.Yes;
   }

      public void button1_Click(object sender, EventArgs e)
   {
       Close();
   }

private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
   {
      e.Cancel = !ShowClose();
   }


better to extract the code in the button1_Click event into a method, then call the method from both events.


Extract the contents of the button method into it's own method and then call the method from both spots.


button1_Click(null, null);


You should not show the MessageBox at all in the button.

You can simply call Application.Exit() in the button, and the FormClosing event can show the confirmation message.


Application.Exit raises the FormClosing event on all open forms:

A FormClosing event is raised for every form represented by the OpenForms property. This event can be canceled by setting the Cancel property of their FormClosingEventArgs parameter to true.

If one of more of the handlers cancels the event, then Exit returns without further action. Otherwise, a FormClosed event is raised for every open form, then all running message loops and forms are closed.

I would rewrite it like this:

public void button1_Click(object sender, EventArgs e)
{
    // Exits the application
    Application.Exit();
}

private void xGameForm_FormClosing(object sender, FormClosingEventArgs e)
{
    //Yes or no message box to exit the application
    DialogResult Response;
    Response = MessageBox.Show("Are you sure you want to Exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
    if (Response == DialogResult.No)
    {
        // Cancel the close, prevents applications from exiting.
        e.Cancel = true;
    } else {
        Application.Exit();
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜