24

i've been searching on google and stackoverflow for 2hours now. There has to be something i am just simply overlooking. Is there an easy way to make the text selectable in a messagebox? As of right now when i call a MessageBox.Show() i can not copy the text displayed. Why not? how would i set the text to be copy able?

my code:

//catch all exceptions
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            //throw;
        }

I want to be able to select the error message that comes out so a user can send it to me and i can troubleshoot their issue. Any help is greatly appreciated.

EDIT: Can not use the crtl-c method. My users are not able to grasp that concept. Need to highlight with mouse and right click to select option. THank you!

EDIT: For reference what i ended up doing is using a mixture of the answers. I created a popup window with a single button and upon the button action i copied to the clipboard. Its not perfect but with the right label it works well enough for now. Thank you all for the suggestions!

//catch all exceptions
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result;

                // Displays the MessageBox.

                result = MessageBox.Show(ex.Message + "\n\nClick OK button to copy to clipboard", "Error", buttons);

                if (result == System.Windows.Forms.DialogResult.OK)
                {

                    Clipboard.SetText(ex.Message);
                    //throw;

                }

            }
toosweetnitemare
  • 2,226
  • 8
  • 33
  • 44
  • 3
    If a user click on Ctrl-C when the MessageBox has focus, the message, the messagebox caption and the buttons labels are copied to the clipboard. – Nasreddine Oct 20 '11 at 18:15
  • 1
    @Nacereddine you are quite correct. However my users are currently not understanding that is a possible option and want the text to be highlightable and copyable :( – toosweetnitemare Oct 20 '11 at 18:17
  • 4
    You need a Custom MessageBox. There are some links provided in the following answer: http://stackoverflow.com/questions/4704839/custom-messagebox/4705061#4705061 – Nasir Oct 20 '11 at 18:17
  • 1
    @toosweetnitemare You could output the messages to a text file and have them email it to you ? to make things easier, you could put the file on their desktop. – Nasreddine Oct 20 '11 at 18:20
  • @Nacereddine that is a great idea. Though i think ill just add a button to the popup that says copy message and copy it to the clipboard for them upon button press. Can you add your answer to the answer list and ill gladly give you credit for the suggestion. – toosweetnitemare Oct 20 '11 at 18:24

6 Answers6

41

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.

Edit: You could output the messages to a text file and have them email it to you ? to make things easier, you could put the file on their desktop

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
7

I would copy your MessageBox's text to the clipboard after or before the user closes the MessageBox using code like this:

var msg = "Hello world!";
MessageBox.Show(msg);
Clipboard.SetText(msg);

This should be easy enough for your users to understand.

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
  • 1
    That isn't working for me. `String message = "There is no Host to ping. Please enter hostname/ip into Field."; MessageBox.Show(message); Clipboard.SetText(message);` just seems to not copy to the clipboard – toosweetnitemare Oct 20 '11 at 18:37
  • @toosweetnitemare Nobody likes to hear this, but your code works on my machine. After running it, I'm able to paste your message into Notepad. I did see someone say they had the same problem you're having on a Windows 7 machine. Their fix was to call this code before calling SetText(): `Clipboard.Clear();` – Jay Riggs Oct 20 '11 at 18:44
  • So very true. I hate hearing "well it works on my machine" haha Thank you for the suggestion of `Clipboard.Clear();` I was just starting to play around with the options of Clipboard. Your suggestion works wonderfully, but for reference, I also got `Clipboard.SetDataObject(message, true);` Thank you for your help! – toosweetnitemare Oct 20 '11 at 18:54
  • 10
    This is a terrible idea. You should never blindly overwrite what the user has on their clipboard without asking them first; you have no idea how important it is or is not. – Dan Bechard Jan 03 '14 at 21:15
7

On all the production systems that I have ever worked on, we create a custom dialog that has a friendly user message with a button to email the error message, the stack trace, a screen shot, and the system information to the support email.

Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • 1
    At the end of this QA you can see an implementation of this, with the Users Action Log included in the support email: http://stackoverflow.com/questions/30326673/user-activity-logging-telemetry-and-variables-in-global-exception-handlers – Jeremy Thompson Feb 06 '17 at 05:10
1

The MessageBox is a window and has a window, so you can use windows api functions to find them. Look at these imports:

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent,
              IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

Note that if you give your MessageBox a title it makes it possible to use FindWindow to find it. Passing the handle returned into FindWindowEx lets you find its child window and GetWindowText would let you read that text. Source Attribution

But don't you already have the contents of the message? You'd kind of have to in order to display it, right?

Tyler
  • 740
  • 9
  • 27
1

As far as I know, This has been asked a lot of time, and the only solution I found if that you can select the message box, copy it (it will get copy), and then you can paste it, and it will paste the contents in something like a nice format...

From default, standard message box has no way to select the text..

gbianchi
  • 2,129
  • 27
  • 35
1

From this post - Copy Text from MessageBox/Msgbox...

you can use Ctrl-C to copy a message from a messagebox.

And from How to allow copying message on MessageBox, you can't programmatically access the text in the default Windows OS message box. You'll need a custom control for that.

Community
  • 1
  • 1
Yuck
  • 49,664
  • 13
  • 105
  • 135