问题
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;
}
}
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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..
回答5:
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.
回答6:
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?
来源:https://stackoverflow.com/questions/7840231/c-sharp-select-text-from-messagebox-show-popup