C# 中的文件管理器问题



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

如果文本窗口中的当前文本未保存(或自打开以来未更改),并且您尝试关闭,打开或创建新文件,则应在新文件/新文档打开之前询问用户是否要保存。答案选项应为"是"、"否"或"取消"。文本是否不变,不应该提出任何问题。

如果文本已更改,则应在窗口标题中用星号(*)表示,例如"文件.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);
            }
        }
    }
}

这段代码将把你带到正确的方向。我把最后确定的工作留给你。

    // 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;
            }
        }
    }

您将找到足够的关键字来谷歌其他详细信息。

相关内容

  • 没有找到相关文章

最新更新