0

我需要写什么才能让它工作?

如果文本窗口中的当前文本未保存(或自打开后未更改)并且您尝试关闭、打开或创建新文件,则应在新文件之前询问用户他/她是否要保存/新文档打开。答案选项应为“是”、“否”或“取消”。文字是否不变,应该没有问题。

如果文本已更改,则应在窗口标题中用星号 (*) 表示,例如。“文件.txt *”。

此外,您应该可以在当前文件名下使用“保存”进行保存。并另存为创建一个新文件。

最后:关闭当前视图的可能性(也可以是打开的文件)

这是我的代码:

namespace filemanager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void closeFile_Click(object sender, EventArgs e)
        {

            if (textBox1.Text.Length != 0)
            {
                DialogResult answr;

                answr = MessageBox.Show("Do you want to save your file before closing?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                if (answr == DialogResult.No)
                {

                    textBox1.Clear();
                    Application.ExitThread();

                }

                if (answr == DialogResult.Yes)
                {
                    saveFile_Click(this, e);
                }

            }

        }

        private void openFile_Click(object sender, EventArgs e)
        {
            openFileFunc.Title = "Open file";
            openFileFunc.InitialDirectory = "C:";
            openFileFunc.FileName = "";
            openFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";

            string Chosen_File = "";

            if (openFileFunc.ShowDialog() != DialogResult.Cancel)
            {

                Chosen_File = openFileFunc.FileName;
                textBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);

            }

        }

        private void saveFile_Click(object sender, EventArgs e)
        {

            saveFileFunc.Title = "Save file";
            saveFileFunc.InitialDirectory = "C:";
            saveFileFunc.FileName = "";
            saveFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";

            string Save_File = "";


            if (saveFileFunc.ShowDialog() != DialogResult.Cancel)
            {

                Save_File = saveFileFunc.FileName;
                textBox1.SaveFile(Save_File, RichTextBoxStreamType.PlainText);

            }
        }

        private void saveAs_Click(object sender, EventArgs e)
        {

            saveFileFunc.Title = "Save file";
            saveFileFunc.InitialDirectory = "C:";
            saveFileFunc.FileName = "";
            saveFileFunc.Filter = "Text Document|*.txt|Word Documents|*.doc";

            string Save_File = "";


            if (saveFileFunc.ShowDialog() != DialogResult.Cancel)
            {

                Save_File = saveFileFunc.FileName;
                textBox1.SaveFile(Save_File, RichTextBoxStreamType.PlainText);

            }
        }

    }
}
4

1 回答 1

0

这段代码将使您朝着正确的方向前进。我把最终确定留给你。

    // flag for holding the Dirty state of the textbox
    private bool IsDirty = false;
    // the setter handles the showing or no showing of a * in the title
    private bool Dirty
    {
        set
        {
            IsDirty = value;
            this.Text = "TextProcessor " + (IsDirty ? "*" : "");
        }
    }

    // hookup textChanged on the (rich)textBox to trigger the Dirty flag
    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        Dirty = true;
    }

    // hookup the formclosing event to Cancel the closing of the form.
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (IsDirty)
        {
            switch (MessageBox.Show("save?", "saving", MessageBoxButtons.YesNoCancel))
            {
                case DialogResult.Cancel:
                case DialogResult.Yes:
                    e.Cancel = true;
                    break;
                default:
                    break;
            }
        }
    }

您会找到足够的关键字来搜索其他详细信息。

于 2014-03-01T14:47:31.207 回答