简体   繁体   English

C# 赢。 表单:向富文本框添加换行符

[英]C# Win. Form : Adding a line break to a richtextbox

I have a richtextbox in which i am filling with formatted text.我有一个富文本框,我在其中填充了格式化文本。 The RTF of richtextbox looks like this:- RichtextboxRTF看起来像这样:-

{\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Bookman Old Style;}}
\viewkind4\uc1\pard\lang1033\b\f0\fs21 Name\b0\tab : John Miller\par
\b Current Address\b0\tab : ABCDEFGHI JKLMNOP QRST UVWXYZ\par
\b Permanent Address\b0\tab : ABCDEFGHI JKLMNOP QRST UVWXYZ\par
\b Temporary Address\b0\tab : ABCDEFGHI JKLMNOP QRST UVWXYZ\par
}

and the OUTPUT of it looks like this:-它的输出看起来像这样:-


Name : John Miller姓名:约翰·米勒
Current Address : ABCDEFGHI JKLMNOP QRST UVWXYZ当前地址: ABCDEFGHI JKLMNOP QRST UVWXYZ
Permanent Address : ABCDEFGHI JKLMNOP QRST 'line break to be added here' UVWXYZ永久地址: ABCDEFGHI JKLMNOP QRST 'line break to be added here' UVWXYZ
Temporary Address : ABCDEFGHI JKLMNOP QRST UVWXYZ临时地址:ABCDEFGHI JKLMNOP QRST UVWXYZ


Now i want to add a line break at text index 119 where line break to be added here is written.现在我想在文本索引 119处添加一个换行符,其中要在此处添加换行符 How do i do it?我该怎么做?

Challanges挑战

  1. You cannot directly use linebreak in text like:您不能直接在文本中使用换行符,例如:

    richTextBox.Text = richTextBox.Text.Substring(0,119) + Environment.NewLine + richTextBox.Text.Substring(119, richTextBox.Text.Length - 119);

    because it will remove the bold settings i have done in text text.因为它将删除我在文本文本中所做的粗体设置

  2. cannot change directly in RTF as we dont know at what index that particular text might be at.不能直接在 RTF 中更改因为我们不知道特定文本可能位于哪个索引。 Suppose the text is at 119 index but the RTF is at 263th index .假设文本位于 119 索引RTF 位于 263 索引

    We also cant use text search to find the index in RTF as all the 3 addresses are exactly the same.我们也不能使用文本搜索来查找 RTF 中的索引,因为所有 3 个地址都完全相同。

This should do it for you:这应该为你做:

rtb1.SelectionStart = 119;
rtb1.SelectionLength = 0;
rtb1.SelectedText = Environment.NewLine;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM