2012-03-10 50 views
4

我的表單中有一個列表框控件,其中包含文件夾中特定類型的文件的路徑。 關於項目雙擊我將頁面動態添加到選項卡控件,並將文件的內容加載到富文本框的對象。現在我想編輯內容並再次保存。但是,當我打開保存的文件時,編輯的內容不會被保存,只有在將文件加載到富文本框時才具有較早的內容。如何更新富文本框對象文本並保存。保存RichTextBox內容不會更新

private void lstErrorList_MouseDoubleClick(object sender, MouseEventArgs e) 
     { 
      ArrayList errorType = new ArrayList(); 
      RichTextBox myrich = new RichTextBox(); 
      string[] list; 

      TabPage selectedTab; 

      if (lstErrorList.Items.Count > 0) 
      { 
       string error = lstErrorList.SelectedItem.ToString(); 
       int result = error.LastIndexOf('\\'); 
       string filename = error.Substring(result + 1, error.Length - (result + 1)); 
       list = error.Split(new char[] { '\t' }); 
       int pagecount; 
       TabPage tp = new TabPage(); 
       pagecount = this.tabControl1.TabPages.Count; 
       bool found = false; 
       foreach (TabPage tab in tabControl1.TabPages) 
       { 
        if (filename.Equals(tab.Name)) 
        { 
         tabControl1.SelectedTab = tab; 
         found = true; 
         break; 
        } 
       } 

       if (!found) 
       { 
        tabControl1.TabPages.Add(filename, filename); 
        tabControl1.SelectedTab = tabControl1.TabPages[tabControl1.TabPages.Count - 1]; 
        int i = tabControl1.TabPages.Count; 
        myrich.Height = this.tabControl1.Height - 30; 
        myrich.Width = this.tabControl1.Width - 10; 
        myrich.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom); 
        tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(myrich); 
        string path = list[7]; 
        objReader = new System.IO.StreamReader(path); 
        myrich.Text = objReader.ReadToEnd(); 
        objReader.Close(); 
       } 

       int val = 0; 
       string val1 = list[3]; 
       string replacement = Regex.Replace(val1, @"\t|\n|\r|[a-zA-Z]", ""); 
       val = Convert.ToInt32(replacement); 
       foreach (Control ct in tabControl1.SelectedTab.Controls) 
       { 
        if (ct is RichTextBox) 
        { 
         RichTextBox x = (RichTextBox)ct; 

         x.Select(val, wordToFind.Length); 
         x.SelectionBackColor = Color.Wheat; 
         x.Focus(); 
         break; 
        } 
       } 
      } 
     } 
private void mnuValidate_Click(object sender, EventArgs e) 
     {      
      myrich.Refresh(); 
      myrich.Update();   
      foreach (TabPage page in tabControl1.TabPages) 
      {     
        string Saved_File = "";     
        saveFD.Title = "Save the file"; 
        saveFD.FileName = ChosenFileName; 
        saveFD.Filter = "Text File|*.txt|Html File|*.html|Xhtml File|*.xhtml|XML File|*.xml"; 
        Saved_File = saveFD.FileName; 
        foreach (Control ct in tabControl1.SelectedTab.Controls) 
        { 
         if (ct is RichTextBox) 
         { 
          int x = tabControl1.SelectedTab.Controls.IndexOf(ct); 
          MessageBox.Show(x.ToString()); 
          ((RichTextBox)page.Controls[x]).SaveFile(Saved_File,RichTextBoxStreamType.RichText);       

         }   
} 


        this.tabControl1.TabPages.Remove(page); 


      }   
      lstErrorList.Items.Clear(); 
      if (filePathlist.Count == 0) 
      { 
       MessageBox.Show("No input files found,Please upload files and validate again", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      else 
      { 
       if (HTML_QC_MultipleFiles.Errors.Checkeditemlist.Count == 0) 
       { 
        MessageBox.Show("Please select the error type and validate again", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
       else 
       { 
        if (singlefile == true) 
        { 
         Validate(); 
        } 
        else 
        { 
         bool errorFound = false; 
         string[] words; 
         foreach (string file in filePathlist) 
         { 
          int lineno, index; 
          objReader = new System.IO.StreamReader(file); 
          myrich.Clear(); 
          myrich.Height = this.tabControl1.Height - 30; 
          myrich.Width = this.tabControl1.Width - 10; 
          myrich.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom); 
          myrich.Text = objReader.ReadToEnd(); 
          ChosenFileName = file; 
          Validate(); 
          objReader.Close(); 
         } 
        } 
       } 
      } 
     } 

回答

2

我認爲這個問題可能您的代碼要求一個文件名通過集中的每個保存到同一個文件的控制另存爲,然後遍歷。如果你有兩個富文本框,那麼保存第一個文件的努力可能會被第二個覆蓋。

看出來其他的事情:

  • 是否有保存過程中的異常?
  • 該文件是否與第一個文件一樣被保存爲同名文件?
  • SaveFile方法僅保存RTF時,爲什麼將對話框過濾器保存爲* .rtf,* .txt和* .html?
  • 路徑操作通常應通過System.IO.Path類來完成。
  • 考慮引入特定活動的方法/函數,以便最終不會出現如此龐大的代碼牆方法。
+1

要保存的文件的名稱與打開文件夾對話框中文件的名稱相同。該文件被覆蓋。沒有例外。是的,這個文件肯定與第一個文件名保存的一樣。我只想保存* .rtf,*。txt,*。html和* .xhtml文件類型的文件。我將這些路徑保存在數組列表中,並在保存時檢索它。 – 2012-03-12 04:42:51

+0

另一件事是,如果我編輯richtextbox的內容然後編輯的內容被保存。就像我創建richtextbox的對象並編輯對象的內容一樣,編輯的內容不會被保存。 – 2012-03-12 04:50:49

+0

@ManojNayak:'編輯對象的內容' - 由哪個屬性編輯其內容? – 2012-03-12 11:40:50