【问题标题】:How can I share an event handler?如何共享事件处理程序?
【发布时间】: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


    【解决方案1】:

    您的事件参数必须采用 FormClosingEventArgs e 形式 更改它,然后重试。

    【讨论】:

    • 非常感谢。很抱歉这篇凌乱的帖子。
    猜你喜欢
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 2016-10-28
    相关资源
    最近更新 更多