Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存时不会更新RichTextBox内容_C#_Winforms - Fatal编程技术网

C# 保存时不会更新RichTextBox内容

C# 保存时不会更新RichTextBox内容,c#,winforms,C#,Winforms,我的表单中有一个列表框控件,其中包含文件夹中特定类型文件的路径。 在项目双击时,我将页面动态添加到选项卡控件,并将文件内容加载到富文本框的对象中。现在我想编辑内容并再次保存。但是,当我打开保存的文件时,编辑的内容不会保存,它只有在将文件加载到富文本框时存在的早期内容。如何更新富文本框对象文本并保存 private void lstErrorList_MouseDoubleClick(object sender, MouseEventArgs e) { A

我的表单中有一个列表框控件,其中包含文件夹中特定类型文件的路径。 在项目双击时,我将页面动态添加到选项卡控件,并将文件内容加载到富文本框的对象中。现在我想编辑内容并再次保存。但是,当我打开保存的文件时,编辑的内容不会保存,它只有在将文件加载到富文本框时存在的早期内容。如何更新富文本框对象文本并保存

 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();
                        }
                    }
                }
            }
        }
我认为问题可能在于,您的代码要求将文件名另存为,然后遍历控件集,将每个控件保存到同一个文件中。如果您有两个富文本框,那么可能是保存第一个富文本框的努力被第二个富文本框覆盖了

其他需要注意的事项:

  • 保存期间是否存在异常
  • 文件是否确实以与第一个文件相同的名称保存
  • SaveFile
    方法仅保存rtf时,为什么要将对话框过滤器保存为*.rtf、*.txt和*.html
  • 路径操作通常应该通过类来完成
  • 考虑为特定的活动引入方法/函数,这样您就不会得到如此庞大的代码墙方法

要保存的文件名与“打开文件夹”对话框中的文件名相同。该文件已被覆盖。没有例外。是的,文件肯定是以与第一个文件相同的名称保存的。我只想保存.rtf、.txt、*.html和*.xhtml文件类型的文件。我将路径保存在arraylist中,并在保存时检索路径。另一件事是,如果我编辑richtextbox的内容,则会保存编辑的内容。其中,就好像我创建了richtextbox的对象并编辑了对象的内容一样,编辑的内容不会被保存。@ManojNayak:“编辑对象的内容”-您通过哪个属性编辑其内容?问题得到了解决。。正如您所说,文件路径中有一个错误。。内容保存在另一个文件中。