Form Closing help

Posted by Michael Quiles on Stack Overflow See other posts from Stack Overflow or by Michael Quiles
Published on 2010-06-01T14:46:25Z Indexed on 2010/06/01 14:53 UTC
Read the original article Hit count: 140

Filed under:

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 the 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 
   }

© Stack Overflow or respective owner

Related posts about c#