【发布时间】:2019-12-08 06:32:32
【问题描述】:
private void ExitAndSave(object sender, EventArgs e) { foreach(MdiChildren 中的表单) { if (form is TextForm tf) { tf.BringToFront(); DialogResult dr = MessageBox.Show("您要保存文档吗?", "保存文档?", 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();
}
此代码运行良好。我需要在用户退出程序时调用它。我无法将此事件分配给设计视图中的表单关闭事件。
【问题讨论】:
标签: windows forms sharing eventhandler