如何弹出窗体关闭事件上的消息框 [英] How Do I Popup A Message Box On Form Close Event

查看:64
本文介绍了如何弹出窗体关闭事件上的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个富文本框



i想要请求用户在表单结束事件中将该数据保存在richtext框中。



i have a richtext box

i want to ask the user to save that data in richtext box on form closing event.

protected override void OnFormClosing(FormClosingEventArgs e) 
{
        base.OnFormClosing(e);
        if (!e.Cancel) {
            if (MessageBox.Show("Really?", "Close", MessageBoxButtons.YesNo) != DialogResult.Yes) 

            {
                e.Cancel = true;
            }
        }
    }





以上是询问用户结束活动的代码。



我在txt文件中保存richtextbox数据的代码是





above is the code for asking user on closing event.

My code for saving richtextbox data in txt file is

<pre lang="cs">public void SaveMyFile()
        {
            //// Create a SaveFileDialog to request a path and file name to save to.
            SaveFileDialog saveFile1 = new SaveFileDialog();

            //// Initialize the SaveFileDialog to specify the RTF extension for the file.
            saveFile1.DefaultExt = "*.txt";
            saveFile1.Filter = "Chat History (*.txt)|*.txt";

            //// Determine if the user selected a file name from the saveFileDialog.
            if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
               saveFile1.FileName.Length > 0)
            {
                //// Save the contents of the RichTextBox into the file.
                richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
            }
        }







如果用户说是,则保存文件并退出



如果用户说没有那么退出而不保存。




if user says yes then save file and exit

if user says no then exit without saving.

推荐答案

这里的正常方法是三种选择- 保存?是/否/取消



无论如何,请尝试:



The normal approach here would be three choices - Save? yes/no/cancel

Anyway for your approach try:

protected override void OnFormClosing(FormClosingEventArgs e) 
{
        base.OnFormClosing(e);
        if (!e.Cancel) {
            if (MessageBox.Show("Do you want to save changes?", "Close", MessageBoxButtons.YesNo) != DialogResult.Yes) 
 
            {
                // e.Cancel = true;
                SaveMyFile();
            }
        }
    }


这篇关于如何弹出窗体关闭事件上的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆