How to copy the contents of a multi-line text field to the clipboard in C #?

I have text coming from a database in a multiline text field, how can I copy it to the clipboard so that the user can paste it into another window or file (for example, from my application into another text field)? OR, if possible, in a notepad / word file.

+6
source share
4 answers
Clipboard.Clear(); //Clear if any old value is there in Clipboard Clipboard.SetText("abc"); //Copy text to Clipboard string strClip = Clipboard.GetText(); //Get text from Clipboard 
+11
source

There is no difference in copying text from one or multi-line TextBox to and from the clipboard using Clipboard.SetText() (and, of course, Clipboard.GetText() ). A TextBox will still contain one String , regardless of whether it contains line breaks or not. This is just eye candy.

In terms of limitations, your Clipboard.SetText() method will always accept only one line, its size is limited only by the amount of free memory at a given time.

To insert this text manually in applications such as Notepad or Word, no special code is required.

Clipboard.SetText(yourTextBox.Text); - that’s all you need.

+2
source

To save lines in text, you must replace the character "\ n" with the character NewLine, as in the example:

  string textforClipboard = TextBox1.Text.Replace("\n", Environment.NewLine); Clipboard.Clear(); Clipboard.SetText(textforClipboard); 
+1
source

All Articles