John Lane
John Lane

Reputation: 3

How can I share an event handler?

private void ExitAndSave(object sender, EventArgs e) { foreach (Form form in MdiChildren) { if (form is TextForm tf) { tf.BringToFront(); DialogResult dr = MessageBox.Show("Do you want to save your document?", "Save document?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (dr == DialogResult.Yes)
                {
                    //counter++;
                    var textBoxValue = tf.FetchTextBoxValue();
                    //string filePath = $"{DateTime.Now.Ticks.ToString()}.txt";
                    string filePath = $"composition{DateTime.Now.Ticks.ToString()}.txt";
                    File.WriteAllText(filePath, textBoxValue);
                }
                if (dr == DialogResult.No)
                {
                    continue;
                }

            }
            else if (form is ImageDocumentForm)
            {
                MessageBox.Show("Please note that only text documents can be saved.", "Advisory:", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }
        Close();
    }

This code works fine. I need it to be called when the user exits the program. I'm not able to assign this event to the form closing event in the design view.

Upvotes: 0

Views: 43

Answers (1)

demoncrate
demoncrate

Reputation: 390

Your event argument needs to be of the form FormClosingEventArgs e Change that and try again.

Upvotes: 0

Related Questions