c# – 保存时不更新RichTextBox内容

c# – 保存时不更新RichTextBox内容,第1张

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

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",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?
>路径 *** 作通常应通过System.IO.Path类完成.>考虑为特定活动引入方法/函数,以便最终不会遇到如此庞大的代码墙方法.

总结

以上是内存溢出为你收集整理的c# – 保存时不更新RichTextBox内容全部内容,希望文章能够帮你解决c# – 保存时不更新RichTextBox内容所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1234480.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存