I have a list control in my form that contains the paths of files of a specific type from a folder. When I double-click an item, I dynamically add the page to the tab control and load the contents of the file into a rich text field object. Now I want to edit the contents and save it again. But when I open the saved file, the edited content is not saved, it contains only the previous content that was there when the file was loaded into the text box. How to update the text of a text field and save the text.
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(); } } } } }
source share