c# 从messagebox.show popup中选择文本

发布于 2024-12-10 22:26:57 字数 1152 浏览 0 评论 0原文

我已经在 google 和 stackoverflow 上搜索了 2 个小时。一定有一些东西我只是忽略了。有没有一种简单的方法可以使消息框中的文本可供选择?截至目前,当我调用 MessageBox.Show() 时,我无法复制显示的文本。为什么不呢?我如何将文本设置为可复制?

我的代码:

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

我希望能够选择出现的错误消息,以便用户可以将其发送给我,我可以解决他们的问题。非常感谢任何帮助。

编辑:不能使用 crtl-c 方法。我的用户无法理解这个概念。需要用鼠标突出显示并右键单击以选择选项。谢谢你!

编辑:作为参考,我最终所做的是使用答案的混合。我创建了一个带有单个按钮的弹出窗口,并根据按钮操作将其复制到剪贴板。它并不完美,但有了正确的标签,目前它的效果就足够好了。谢谢大家的建议!

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

                }

            }

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;

                }

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夕色琉璃 2024-12-17 22:26:58

我会在用户关闭 MessageBox 之后或之前使用如下代码将 MessageBox 的文本复制到剪贴板:

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

这对于您的用户来说应该很容易理解。

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.

江南烟雨〆相思醉 2024-12-17 22:26:58

在我曾经参与过的所有生产系统上,我们创建了一个自定义对话框,其中包含一条友好的用户消息,其中包含一个按钮,用于通过电子邮件将错误消息、堆栈跟踪、屏幕截图和系统信息发送到支持电子邮件。

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.

寂寞清仓 2024-12-17 22:26:58

据我所知,这已经被问了很多次了,我找到的唯一解决方案是,您可以选择消息框,复制它(它将得到复制),然后您可以粘贴它,它就会粘贴内容格式不错...

默认情况下,标准消息框无法选择文本..

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

罪#恶を代价 2024-12-17 22:26:58

从这篇文章 - 从 MessageBox/Msgbox 复制文本...

您可以使用 Ctrl-C 从消息框中复制消息。

如何允许在 MessageBox 上复制消息中,您不能以编程方式访问默认 Windows 操作系统消息框中的文本。为此,您需要一个自定义控件。

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.

可可 2024-12-17 22:26:58

MessageBox是一个窗口并且有一个窗口,所以你可以使用windows api
函数来找到它们。查看这些导入:

[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);

请注意,如果您为 MessageBox 指定一个标题,则可以使用 FindWindow 来查找它。将返回的句柄传递给 FindWindowEx 可以让您找到其子窗口,而 GetWindowText 则可以让您读取该文本。
来源归属

但是您不是已经有了邮件的内容吗?你必须这样做才能显示它,对吗?

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?

二手情话 2024-12-17 22:26:57

如果用户在 MessageBox 具有焦点时按 Ctrl-C,则消息、MessageBox 标题和 MessageBoxButtons 标签将复制到剪贴板。

编辑:您可以将消息输出到文本文件并让他们通过电子邮件发送给您吗?为了让事情变得更容易,您可以将该文件放在他们的桌面上

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文