Pergunta

What do I need to write to get this to work?

If the current text in the text window is not saved (or is unchanged since it was opened) and you try to close, open or create a new file, the user should be asked if he / she wants to save before the new file / new document is opened. The answer options should be "yes", "no" or "cancel". Is the text unchanged, no question should come up.

If the text has been changed it shall be indicated by an asterisk (*) in window title, ex. "File.txt *".

Also, you should be able so save with "Save" under the current filename. And Save As to create a new file.

And last: The possibility to close the current view (can also be an opened file)

Here is my code:

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);

            }
        }

    }
}
Foi útil?

Solução

This snippet of code will put you in the right direction. I leave the finalization to you.

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

You will find enough keywords to google the additional details.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top