How to allow copying message on MessageBox

后端 未结 5 1797
無奈伤痛
無奈伤痛 2021-02-06 20:52

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

相关标签:
5条回答
  • 2021-02-06 20:54

    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.

    0 讨论(0)
  • 2021-02-06 20:54

    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   
        ---------------------------
    
    0 讨论(0)
  • 2021-02-06 21:04

    If you're displaying the messagebox...

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

    will copy the item to the clipboard.

    0 讨论(0)
  • 2021-02-06 21:13

    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.

    0 讨论(0)
  • 2021-02-06 21:13

    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.

    0 讨论(0)
提交回复
热议问题