4

I have a program that contains a list of names, and a button that displays a random name in a MessageBox. Is there any way I can add a button "Copy" next to the OK to the MessageBox, that when clicked copies the name and then closes?

If the above isn't possible, is there a way to enable copying the text in a MessageBox?

Thank you.

edit: My users won't understand Ctrl+C, Highlight and Right Click > Copy is what I'm looking for (if a Copy button isn't possible)

Abraham
  • 1,179
  • 2
  • 12
  • 22
  • 1
    I have a vague memory that Windows message boxes support CTRL-C natively. Can't confirm it right now though... – Fredrik Mörk Apr 12 '13 at 20:28
  • @FredrikMörk: They do – Pondidum Apr 12 '13 at 20:49
  • Even once you figure out the code part, your next question is going to be how to add a custom button to the MessageBox. That's possible, but not at all easy. You might be better off creating your own message box. – Cody Gray Apr 12 '13 at 21:41

2 Answers2

4

If a user presses Ctrl-C while the MessageBox has focus, the message, the MessageBox caption and the MessageBoxButtons labels are copied to the clipboard.

I googled your title and found this..

Or if you really need a button that says copy you can create your own MessageBox with a new windows form and then do what you want with the buttons. Open it like this to keep the MessageBox feel :

var myMessageBox = new CustomMessageBox();
myMessageBox.ShowDialog();
Community
  • 1
  • 1
phadaphunk
  • 12,129
  • 15
  • 69
  • 106
  • I want it to be easier for my users to copy text. I'll take a look at that, but I'm mainly interested if I can add a button to copy the text – Abraham Apr 12 '13 at 20:29
1

It sounds like maybe you are looking for the Clipboard class.

Clipboard.SetText(variableWithValue);

There is also another answer here about manipulating the contents of a Message Box.

It also might be easier to simply make a modal dialog that emulates a MessageBox without actually using the MessageBox class.

Community
  • 1
  • 1
AJ Henderson
  • 1,040
  • 11
  • 31