11

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

leppie
  • 115,091
  • 17
  • 196
  • 297
Paz
  • 694
  • 1
  • 5
  • 14

4 Answers4

19
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
andy
  • 5,979
  • 2
  • 27
  • 49
3

There is no difference in copying text from a single or multiline TextBox to and from the clipboard using Clipboard.SetText() (and of course Clipboard.GetText()). A TextBox will still contain a single String, whether it contains line breaks or not. That's only eye candy.

From a limitations perspective, your ClipBoard.SetText() method will always only accept one single string as well, its size only limited by and to the amount of free memory at that given time.

No special code is needed to paste this text manually into applications like Notepad or Word.

Clipboard.SetText(yourTextBox.Text); is all you need.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
2

For saving lines in text you should replace "\n" to NewLine character, as in example:

 string textforClipboard = TextBox1.Text.Replace("\n", Environment.NewLine);
 Clipboard.Clear();
 Clipboard.SetText(textforClipboard);
hotenov
  • 911
  • 1
  • 11
  • 17
0

System.Windows.Forms.Clipboard.SetText(..)

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.settext.aspx

Prescott
  • 7,312
  • 5
  • 49
  • 70