Click here to Skip to main content
16,016,925 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I needed a text control that provided a one-way copy to clipboard function while also being able to format specific lines and part of lines so I tried the RichTextBox. This is working pretty well. The problem is when I perform a copy and paste operation to the clipboard. All the the line breaks ('\n') are removed and the copied text is run together on a single line.

For instance:
This is line 1.
This is line 2.
This is line 3.

Becomes:
This is line 1.This is line2.This is line 3.

The text of the RichTextBox is being populated from code-behind due to the very dynamic nature of what is being displayed.

C#
// example
var myDoc = new FlowDocument();

foreach (MyDataObject dObj in dataFromDatabase)
{
  var para = new Paragraph();
  para.Inlines.Add(new Run(dObj.DataPoint1.ToString()));
  if (dObj.DataPoint2 > 3)
  {
    para.Inlines.Add(new Run("\nThis exceeds reporting threshold for blah, blah, blah,...")); // \n or \r it doesn't matter, they are both removed in copy/paste
  }
  // and other lines as required in the program logic
  
  myDoc.Blocks.Add(para);
}

myRichTextBox.Document = myDoc;


What I have tried:

I have searched the internet for how to prevent this but all I have found is substituting a carriage return ('\r') for the line break but it also did not work.
Posted
Updated 23-Sep-20 5:37am
Comments
Michael_Davies 29-Mar-17 16:23pm    
Did you find this;

https://www.experts-exchange.com/questions/26895919/C-RichTextBox-contents-to-clipboard.html#a35180339
Foothill 29-Mar-17 16:33pm    
Yes I had come across that answer but I didn't see how that solution could apply. However, taking a third look did lead me to the solution which I will post up shortly.

I found the problem, the problem is not with '\r' or '\n' but with their position in the text. If you enter the characters like "\n\r", the RichTextBox starts double spacing the lines and a copy\paste operation runs the lines together. Now, if you reverse their positions like "\r\n", the RichTextBox displays them correctly and the copy\paste operation works like it should. I need to do more research to figure out why that matters but it fixed the problem.
 
Share this answer
 
Comments
Richard Deeming 30-Mar-17 13:03pm    
Windows always expects the carriage-return (\r) to precede the line-feed (\n). Other systems might use just a carriage-return (Macs), or just a line-feed (UNIX, Linux). But I don't think any system uses a line-feed followed by a carriage return.

Which explains why the RichTextBox would double-space the lines, but not obviously why the copy/paste would strip them out.
Member 14263458 16-Apr-19 3:35am    
I am having the same problem you describe - but seeing a weird difference I wonder if you could explain?

If I take the multi-line text that my app generates and put it on the clipboard, when I paste it into the Messages app, it all looks fine, but if I paste it into the Notes app or to a Facebook message - the linefeeds are almost all stripped (the first 3 work). This is all occurring on the same device (an iPhone 8+ with iOS 12). I am currently only using \n.

Any idea what’s going on?
add AcceptsReturn="True" to your texbox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900