简体   繁体   中英

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:-

{\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
Permanent Address : ABCDEFGHI JKLMNOP QRST 'line break to be added here' UVWXYZ
Temporary Address : 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. 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. Suppose the text is at 119 index but the RTF is at 263th index .

    We also cant use text search to find the index in RTF as all the 3 addresses are exactly the same.

This should do it for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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