Copying formatted text into clipboard

4.3k views Asked by At

I have strange problem with coping text into clipboard. I want to copy text from textbox with additional formatting. In order to do that I intercept KeyDown event on textbox and I check if keys CTRL and C are pressed. Then I do sth like that

 string extraFormatedText = formatText(txtBox.Text);
 Clipboard.SetText(extraFormatedText, TextDataFormat.Text);

Function formatText adds couple of empty additional lines in text. However if I paste this text into notepad there is no additional formatting. Why is that ??

If I call function Clipboard.GetText() I can clearly see that in returned string there are extra characters ( \n \r).

2

There are 2 answers

0
JYelton On

\r represents a carriage return and \n a newline (linefeed) character.

The method formatText must be adding these to the end of the string.

Also, the program Notepad has no formatting functionality such as bold or italic, it simply displays everything in the chosen font, size and style.

0
Vitaliy Kurokhtin On

If you're adding just \n to insert new lines Notepad will not understand that as it expects \r\n as a line break. Try pasting your text into Notepad++ and you'll see that line breaks are actually there.

It's safer to use Environment.NewLine, which on Windows will give you exactly \r\n.