Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem pasting text in certain apps
 

Problem pasting text in certain apps

In a project using a RichTextBox, when I copy text and paste it into some other applications, the line breaks are missing. This happens in MS NotePad and the sample text area in Expresso. The text pastes correctly (line breaks are present) with WordPad, NoteTab Lite and NotePad++.

Here's the code used to copy text (RichTextBox is named "rtf"):

Private Sub menuEdit_Copy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles menuEdit_Copy.Click, tb_Copy.Click, popup_Copy.Click
Try
If rtf.SelectedText.Length > 0 Then
Clipboard.SetText(rtf.SelectedText, TextDataFormat.Text)
End If
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Exclamation, title)
End Try
End Sub

Any ideas why this happens?

Thanks
AB

neverhome  Saturday, August 22, 2009 4:20 AM
That makes some sense. RichTextBox is very unusual among Windows controls. Its line endings are "\n", every other code in Windows uses "\r\n". Although I don't repro, there's a good chance that some controls don't handle "\n" well. If SetText() is preferred, you can probably fix the problem with Replace("\n", "\r\n").

Hans Passant.
nobugz  Saturday, August 22, 2009 12:49 PM
All of them or just the last one? What version of Windows?

Hans Passant.
nobugz  Saturday, August 22, 2009 11:49 AM
Looks like using RichTextBox.Copy() instead of ClipBoard.SetText() solves the problem. I was told by a member over at CodeProject that RichTextBox.Copy() deals properly with newline chrs, while ClipBoard.SetText() doesn't always provide the receiving application the info it needs to recognize the ends of the lines.

Thanks
AB
neverhome  Saturday, August 22, 2009 12:32 PM
That makes some sense. RichTextBox is very unusual among Windows controls. Its line endings are "\n", every other code in Windows uses "\r\n". Although I don't repro, there's a good chance that some controls don't handle "\n" well. If SetText() is preferred, you can probably fix the problem with Replace("\n", "\r\n").

Hans Passant.
nobugz  Saturday, August 22, 2009 12:49 PM

You can use google to search for other answers

Custom Search

More Threads

• How do you set a variable that can be set by one form and accessed by another form
• Close button for tab control
• I am new to programming in C# and would very much appreciate some help on creating an array of text boxes
• Reg:MSMQ 3.0 for xp
• Calculate time with int
• Working with DataGridView.UserAddedRow
• Am I connected to the Internet?
• Printing and debugging a form
• Dynamically adding controls to FlowLayoutPanel
• MessageBox Centered From Parent