How to allow copying message on MessageBox

C#.NetWpf

C# Problem Overview


How can I allow selecting and copying of text from MessageBox in WPF?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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   
    ---------------------------

Solution 3 - C#

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.

Solution 4 - C#

If you're displaying the messagebox...

System.Windows.Forms.Clipboard.SetDataObject(messageToShowInMsgBoxString, true);

will copy the item to the clipboard.

Solution 5 - C#

The best approach would be to use a Window with a selectable text control, like a textbox for example. I can say from experience that this is the easiest way, and will not take much time or code changes to implement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionns12345View Question on Stackoverflow
Solution 1 - C#JoeyView Answer on Stackoverflow
Solution 2 - C#danielcooperxyzView Answer on Stackoverflow
Solution 3 - C#GadyCView Answer on Stackoverflow
Solution 4 - C#John K.View Answer on Stackoverflow
Solution 5 - C#A.R.View Answer on Stackoverflow