如何在 C# 中创建 MessageBox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49147/
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-03 10:01:45  来源:igfitidea点击:

How do I create a MessageBox in C#?

提问by Nick Stinemates

I have just installed C# for the first time, and at first glance it appears to be very similar to VB6. I decided to start off by trying to make a 'Hello, World!' UI Edition.

我也是第一次安装C#,乍一看好像和VB6很像。我决定从尝试制作“Hello, World!”开始。用户界面版。

I started in the Form Designer and made a button named "Click Me!" proceeded to double-click it and typed in

我从表单设计器开始,制作了一个名为“点击我!”的按钮。继续双击它并输入

MessageBox("Hello, World!");

I received the following error:

我收到以下错误:

MessageBox is a 'type' but used as a 'variable'

MessageBox 是一种“类型”,但用作“变量”

Fair enough, it seems in C# MessageBox is an Object. I tried the following

很公平,在 C# MessageBox 中似乎是一个对象。我尝试了以下

MessageBox a = new MessageBox("Hello, World!");

I received the following error: MessageBox does not contain a constructor that takes '1' arguments

我收到以下错误:MessageBox 不包含采用“1”参数的构造函数

Now I am stumped. Please help.

现在我被难住了。请帮忙。

采纳答案by Merus

MessageBox.Show also returns a DialogResult, which if you put some buttons on there, means you can have it returned what the user clicked. Most of the time I write something like

MessageBox.Show 还返回一个 DialogResult,如果您在其中放置一些按钮,则意味着您可以让它返回用户单击的内容。大多数时候我会写一些类似的东西

if (MessageBox.Show("Do you want to continue?", "Question", MessageBoxButtons.YesNo) == MessageBoxResult.Yes) {
     //some interesting behaviour here
}

which I guess is a bit unwieldy but it gets the job done.

我想这有点笨拙,但它可以完成工作。

See https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresultfor additional enum options you can use here.

有关您可以在此处使用的其他枚举选项,请参阅https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresult

回答by moobaa

Code summary:

代码摘要:

using System.Windows.Forms;

...

MessageBox.Show( "hello world" );

Also (as per this other stack post): In Visual Studio expand the project in Solution Tree, right click on References, Add Reference, Select System.Windows.Formson Framework tab. This will get the MessageBox working in conjunction with the using System.Windows.Forms reference from above.

另外(根据其他堆栈帖子):在 Visual Studio 中展开解决方案树中的项目,右键单击引用,添加引用,System.Windows.Forms在框架选项卡上选择。这将使 MessageBox 与上面的 using System.Windows.Forms 参考结合使用。

回答by George Mauer

It is a static function on the MessageBox class, the simple way to do this is using

它是 MessageBox 类上的一个静态函数,执行此操作的简单方法是使用

MessageBox.Show("my message");

in the System.Windows.Forms class. You can find more on the msdn page for this here. Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message"); or confirm("my question"); functions.

在 System.Windows.Forms 类中。您可以在此处的 msdn 页面上找到更多信息。您可以控制消息框文本、标题、默认按钮和图标等。由于您没有指定,如果您尝试在网页中执行此操作,您应该查看触发 javascript alert("my message"); 或确认(“我的问题”);职能。

回答by William Fitzmaurice

In the System.Windows.Formsclass, you can find more on the MSDNpage for this here. Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message");or confirm("my question");functions.

System.Windows.Forms课堂上,您可以在MSDN页面上找到更多信息。除其他外,您还可以控制消息框文本、标题、默认按钮和图标。由于您没有指定,如果您尝试在网页中执行此操作,您应该查看触发 javascriptalert("my message");confirm("my question");函数。

回答by user2680314

This is some of the things you can put into a message box. Enjoy
MessageBox.Show("Enter the text for the message box",
"Enter the name of the message box",
(Enter the button names e.g. MessageBoxButtons.YesNo),
(Enter the icon e.g. MessageBoxIcon.Question),
(Enter the default button e.g. MessageBoxDefaultButton.Button1)

More information can be found here

这是您可以放入消息框中的一些内容。享受 更多信息可以在这里找到
MessageBox.Show("Enter the text for the message box",
"Enter the name of the message box",
(Enter the button names e.g. MessageBoxButtons.YesNo),
(Enter the icon e.g. MessageBoxIcon.Question),
(Enter the default button e.g. MessageBoxDefaultButton.Button1)

回答by Sagar Dev Timilsina

Try below code:

试试下面的代码:

MessageBox.Show("Test Information Message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);

回答by KeithJ

I got the same error 'System.Windows.Forms.MessageBox' is a 'type' but is used like a 'variable', even if using:

我得到了同样的错误'System.Windows.Forms.MessageBox' is a 'type' but is used like a 'variable',即使使用:

MessageBox.Show("Hello, World!");

I guess my initial attempts with invalid syntax caused some kind of bug and I ended up fixing it by adding a space between "MessageBox.Show" and the brackets ():

我想我最初使用无效语法的尝试导致了某种错误,我最终通过在“MessageBox.Show”和括号 () 之间添加一个空格来修复它:

MessageBox.Show ("Hello, World!");

Now using the original syntax without the extra space works again:

现在使用没有额外空间的原始语法再次起作用:

MessageBox.Show("Hello, World!");

回答by MicroDOS

Also you can use a MessageBoxwith OKCanceloptions, but it requires many codes. The ifblock is for OK, the elseblock is for Cancel. Here is the code:

您也可以使用MessageBoxwithOKCancel选项,但它需要很多代码。该if块是OK,该else块是用于Cancel。这是代码:

if (MessageBox.Show("Are you sure you want to do this?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
    MessageBox.Show("You pressed OK!");
}
else
{
    MessageBox.Show("You pressed Cancel!");
}

You can also use a MessageBoxwith YesNooptions:

您还可以使用MessageBoxYesNo选项:

if (MessageBox.Show("Are you sure want to doing this?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    MessageBox.Show("You are pressed Yes!");
}
else
{
    MessageBox.Show("You are pressed No!");
}