Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy the contents of a Multiline textbox to the clipboard in C#?

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.

like image 582
Paz Avatar asked Jan 04 '13 05:01

Paz


People also ask

How to copy TextBox text to clipboard in c#?

To copy the contents of a textbox either use TextBox. Copy() or get text first and then set clipboard value: Clipboard. SetText(txtClipboard.

How do I copy text to clipboard?

To quickly copy selected text or images to the clipboard, use hotkeys Ctrl+C or Ctrl+Insert. These hotkeys work in all Windows programs. Alternatively, you can invoke a pop-up menu by right-clicking on the selected text, and then click Copy.


3 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
like image 112
andy Avatar answered Sep 19 '22 16:09

andy


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.

like image 22
Wim Ombelets Avatar answered Sep 18 '22 16:09

Wim Ombelets


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);
like image 44
hotenov Avatar answered Sep 21 '22 16:09

hotenov