C#中filemanager的问题

时间:2014-03-01 12:38:51

标签: c# file dialog filenames word-processor

我需要写什么才能让它发挥作用?

  

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

     

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

            }
        }

    }
}

1 个答案:

答案 0 :(得分: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;
            }
        }
    }

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