How to allow copying message on MessageBox

I did it this way:

string msgtext = "message text";
if (MessageBox.Show(msgtext, "bla bla bla. (OK to copy)", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
  { Clipboard.SetText(msgtext); }

It works pretty good.


You can just use Ctrl+C while the message box has focus, but it will give you a lot more text than just the error message.

e.g.

    MessageBox.Show("Message", "Message Title", MessageBoxButton.OK);

Would copy and paste as:

    ---------------------------
    Message Title 
    ---------------------------
    Message
    ---------------------------
    OK   
    ---------------------------

If you don't need selecting text as a requirement, just use System.Windows.Forms.MessageBox. It maps to the system-default one which already allows copying its contents with Ctrl+C.

Tags:

C#

.Net

Wpf