C# 如何在 MessageBox 中显示 textBox 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16811859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:50:44  来源:igfitidea点击:

how to display textBox control in MessageBox?

c#.netwinforms

提问by Michael

Any idea how to display textBox control in MessageBox.

知道如何在 MessageBox 中显示 textBox 控件。

I'm working on winforms projcet c#.

我正在研究 winforms projcet c#。

Thank you in advance.

先感谢您。

采纳答案by krillgar

You can't. MessageBox is a special container designed to only show a message and buttons. Instead, you can create your own Form with whatever controls you want, and use .DisplayModal()on it.

你不能。MessageBox 是一个特殊的容器,旨在仅显示消息和按钮。相反,您可以使用您想要的任何控件创建自己的表单,并.DisplayModal()在其上使用。

回答by Massimiliano Peluso

you could create a classic win form that looks like a message box and opening it as a modal form perhaps using Form.ShowDialog

您可以创建一个看起来像消息框的经典获胜表单,并将其作为模态表单打开,也许使用 Form.ShowDialog

more info at

更多信息在

http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

回答by Jahan

As I know there is no way to do that.

据我所知,没有办法做到这一点。

You can create a winform change it's style to look like a MessageBox and add your own controls.

您可以创建一个 winform,将其样式更改为 MessageBox 并添加您自己的控件。

回答by Taj

It will be better to add a new Form in you application which you can customize the way you want.

最好在您的应用程序中添加一个新表单,您可以按照自己的方式自定义。

and just call it from where ever required.

只需从需要的地方调用它。

回答by Jibяa? Kha?

You cannot customise the MessageBox, its better you use a popup designed using Windows Form separately and use its instance to invoke.

您无法自定义 MessageBox,最好使用单独使用 Windows 窗体设计的弹出窗口并使用其实例进行调用。

customPopup popup = new customPopup();
popup.ShowDialog();

Place your controls on the popup form and set their access modifiers to public if you want to access the textboxes or labels etc in your previous form.

如果您想访问之前表单中的文本框或标签等,请将您的控件放在弹出表单上并将它们的访问修饰符设置为 public。

customPopup popup = new customPopup();
popup.msgLabel.Text= "Your message";
popup.ShowDialog();

回答by ZZZ

Yes, as krillgar mentioned,you should create your own form. And 1. Encapsulate the form in a static class or function, so you may just call MyMessageBox.Show(). 2. The textbox should have readonly=true, so the end users won't be able to change the text displayed, while they could select text and copy to clipboard.

是的,正如 krillgar 提到的,您应该创建自己的表单。并且 1. 将表单封装在静态类或函数中,因此您可以只调用 MyMessageBox.Show()。2. textbox 应该有 readonly=true,所以最终用户将无法更改显示的文本,而他们可以选择文本并复制到剪贴板。

Regarding to item 2, I believe many Windows build applications and MS Office use such approach.

关于第 2 条,我相信许多 Windows 构建应用程序和 MS Office 都使用这种方法。

回答by Erfan

You can simply add an Input box from VB.NET into your C# project. First add Microsoft.VisualBasicto your project References, then use the following code:

您可以简单地从 VB.NET 添加一个输入框到您的 C# 项目中。首先将Microsoft.VisualBasic添加到您的项目引用中,然后使用以下代码:

string UserAnswer = Microsoft.VisualBasic.Interaction.InputBox("Your Message ", "Title", "Default Response");

And that should work properly.

这应该可以正常工作。

回答by B.Syal

Solution in here, you can create windows form and design it, set form is dialog, when you call form, it is auto show. In form you design, you set value some parameter static where other class in project, but you should set when you close form design that, OK, come back form init call show dialog, you create interval call when have == null return call, when != null you stop call back and using parameter in class static it !

解决方案在这里,你可以创建windows窗体并设计它,设置窗体是对话框,当你调用窗体时,它是自动显示。在你设计的表单中,你在项目中的其他类中设置了一些参数静态的值,但是你应该在关闭表单设计时设置,好的,返回表单初始化调用显示对话框,当有==空返回调用时创建间隔调用,当 != null 时,您将停止回调并在类 static 中使用参数!