Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote an app which produces a few (3 to 5) lines (strings) of output.
As this text should be displayed I copied it into a listBox and it works as intended.
But I'm looking for a possibility to copy the content into the clipboard allowing the user to insert this text snippet into a file.
I can select the whole content with SelectionMode "MultiExtended" but I didn't succeed transferring it into the clipboard.
This looks like a trivial problem but nevertheless I'm stuck.
How can I solve my problem? Should I use another control than listBox for this task?

What I have tried:

See description of the problem.
The relevant part of my code is quite simple:

My code is quite simple:
string[] ncBlocks = new string[geoelements.Length];
     for (int i = 0; i < geoelements.Length; i++)
     {
         geoelements[i].Translate(-schneidenradius, -schneidenradius);
         geoelements[i].Mirror(lage == 1 || lage == 4, lage == 1 || lage == 2);
         geoelements[i].Translate(abszisseNull, ordinateNull);
         ncBlocks[i] = geoelements[i].CreateNCBlock(format);
         Console.WriteLine(ncBlocks[i]);
     }
     listBox1.Items.AddRange(ncBlocks); // <== This content I want to copy

The text contained in listBox1 is dsplayed as intended. And if the user wants to transfer this text into a file he should be able to copy and paste it.
Maybe there is an event I could use to write the into the clipboard if the text has been selected?
Posted
Updated 11-Apr-20 6:52am
v4
Comments
Richard MacCutchan 11-Apr-20 11:56am    
Without seeing your code it is difficult to guess what your problem is. Saving any amount of text to the clipboard is certainly quite trivial.

Try one of these:

For Winforms: System.Windows.Forms.Clipboard.SetText(yourText);
For WPF: System.Windows.Clipboard.SetText(yourText);
 
Share this answer
 
Comments
Maciej Los 11-Apr-20 13:50pm    
5ed!
Try this:
C#
string[] lines = { "line 1", "line 2", "line 3", "line 4", "line 5" };
Clipboard.SetText(string.Join(Environment.NewLine, lines));
 
Share this answer
 
Comments
Maciej Los 11-Apr-20 13:50pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900