2014-03-01 18 views
0

我需要寫些什麼才能使其工作?C#中filemanager的問題#

如果文本窗口中的當前文本不保存(或因爲它被打開不變),並嘗試關閉,打開或創建一個新的文件,用戶應該問,如果他/她想要在新文件/新文檔打開之前保存。答案選項應該是「是」,「否」或「取消」。文本是否保持不變,不會出現問題。

如果文字已被更改,則應在窗口標題中用星號(*)表示,例如: 「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); 

      } 
     } 

    } 
} 
+0

那麼你的問題是什麼?哪些代碼不起作用? – 2014-03-01 13:26:35

+0

哈哈哎呀,我錯誤地自言自語。我甚至不知道如何寫它。我嘗試了一堆,但我所做的是搞砸了我有的代碼(工作)。 – SwaX

回答

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

你會發現足夠的關鍵字谷歌的額外細節。

+0

謝謝! :)這是如此有幫助! – SwaX