[英]Keep text positions after saving richtextbox to RTF file
我正在尝试通过richtextbox内容(从RTF文件加载)创建一些RTF文件,并通过一些代码打开文件并应用一些更改并将新文件(输出)保存回另一个位置。 我发现位置文字已更改,字体颜色也已更改。 请查看附件截图以获取更多说明。 输入:
输出:
期望的输出
我想我需要谈谈我适用于代码输入的内容:好吧,我需要用数据库中的某些变量替换以$开头的每个变量:
我将这部分代码用于此目的:
foreach (Match match in Regex.Matches(richTextBox1.Text, "(\\$\\w+)"))
{
if (match.Groups[1].Value.Substring(1).Equals("Add1"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress1(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("Add2"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress2(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("Add3"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAdress3(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("Add4"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getLand(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("Rechnr"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, nums_res[j]);
if (match.Groups[1].Value.Substring(1).Equals("Datum"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getDatum(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("resname"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getName1(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("resvorname"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getVorname(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("resroom"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getZimmer(nums_res[j].ToString()));
if (match.Groups[1].Value.Substring(1).Equals("anz"))
richTextBox1.Text = richTextBox1.Text.Replace(match.Groups[1].Value, getAnz(nums_res[j].ToString()));
}
int indexToText = richTextBox1.Find(getAdress1(nums_res[j].ToString()));
int endIndex = richTextBox1.Find(getAdress1(nums_res[j].ToString()));
if(indexToText > 0 && endIndex > 0)
richTextBox1.Select(indexToText, endIndex); richTextBox1.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Center;
int indexToText2 = richTextBox1.Find(getLand(nums_res[j].ToString()));
int endIndex2 = richTextBox1.Find(getLand(nums_res[j].ToString()));
if (indexToText2 > 0 && endIndex2 > 0)
richTextBox1.Select(indexToText2, endIndex2); richTextBox1.SelectionAlignment = System.Windows.Forms.HorizontalAlignment.Center;
richTextBox1.SaveFile("d:\\ryadmogadoroutput" + nums_res[j].ToString());
i = i + 1;
richTextBox1.Clear();
现在,我不会全部替换掉,而是仅替换一部分,我想知道我的输出和所需输出之间的区别在哪里。 (如您所见,我尝试将代码中的第一个变量居中)
我想知道是否有一种方法可以在应用我所说的更改后保持文件的原始格式
谢谢
编辑:将属性.text更改为rtf之后,此处将输出改善。 仍然有一些要修复的细节:字体颜色和一些替换的字符串(我不知道为什么),请参见日期:$ datum
编辑:@TaW的解决方案建议:输出:
编译错误:
我认为您应该通过将“ richTextBox1.Text”更改为“ richTextBox1.RTF”来将文本格式更改为RTF格式
在将任何格式应用于RichText之后, 切勿直接更改Text
。 相反,您需要使用Copy
, Paste
, Append
和其他专门方法严格处理SelectedText
。
因此,至少您需要编写一个适当的Replace
函数!
也许有点运气,虽然不能在Text
上工作,但Rtf
属性也会有所帮助,但是它可能与可能妨碍格式化的东西交织在一起。
这是一个可以做到的功能:
void RtfReplace(RichTextBox RTB, Match match, string replace, ref int offset)
{
RTB.Select(match.Index + offset, match.Length);
RTB.SelectedText = replace;
offset += replace.Length - match.Length;
}
还有几个电话
int offset = 0;
foreach (Match match in Regex.Matches(richTextBox1.Text, "(\\$\\w+)"))
{
if (match.Groups[1].Value.Substring(1).Equals("Add1"))
RtfReplace(richTextBox1, match, getAdress1(nums_res[j].ToString()), ref offset);
if (match.Groups[1].Value.Substring(1).Equals("Add2"))
RtfReplace(richTextBox1, match, getAdress2(nums_res[j].ToString()), ref offset);
//..
更新 :由于每次替换都有可能更改替换文本的长度,因此我们需要根据该差异来调整匹配集合中的所有位置(或重复调用整个替换代码,直到找不到更多匹配为止。)不过简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.