0

これを機能させるには何を書く必要がありますか?

テキスト ウィンドウ内の現在のテキストが保存されていない (または開いてから変更されていない) 場合に、新しいファイルを閉じる、開く、または作成しようとすると、ユーザーは新しいファイルを保存する前に保存するかどうかを尋ねられる必要があります /新しいドキュメントが開かれます。回答の選択肢は、「はい」、「いいえ」、または「キャンセル」である必要があります。テキストは変更されていませんか、質問は出てこないはずです。

テキストが変更されている場合は、ウィンドウ タイトルにアスタリスク (*) で示されます。「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);

            }
        }

    }
}
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 に答える