简体   繁体   English

如何在C#中创建MessageBox?

[英]How do I create a MessageBox in C#?

I have just installed C# for the first time, and at first glance it appears to be very similar to VB6. 我刚刚第一次安装了C#,乍一看它似乎与VB6非常相似。 I decided to start off by trying to make a 'Hello, World!' 我决定先尝试制作一个'Hello,World!'。 UI Edition. UI版。

I started in the Form Designer and made a button named "Click Me!" 我从表单设计器开始,创建了一个名为“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. 很公平,似乎在C#MessageBox中是一个Object。 I tried the following 我尝试了以下内容

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. 请帮忙。

MessageBox.Show also returns a DialogResult, which if you put some buttons on there, means you can have it returned what the user clicked. MessageBox.Show还返回一个DialogResult,如果你在那里放一些按钮,意味着你可以让它返回用户点击的内容。 Most of the time I write something like 大多数时候我写的东西都像

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.dialogresult for additional enum options you can use here. 有关您可以在此处使用的其他枚举选项,请参阅https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresult

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.Forms on Framework tab. 另外( 根据其他堆栈帖子 ):在Visual Studio中展开Solution Tree中的项目,右键单击References,Add Reference,在Framework选项卡上选择System.Windows.Forms This will get the MessageBox working in conjunction with the using System.Windows.Forms reference from above. 这将使MessageBox与上面使用的System.Windows.Forms引用一起工作。

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. 在System.Windows.Forms类中。 You can find more on the msdn page for this here . 您可以在msdn页面上找到更多信息 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"); 由于您没有指定,如果您尝试在网页中执行此操作,则应该查看触发javascript警报(“我的消息”); or confirm("my question"); 或确认(“我的问题”); functions. 功能。

试试以下代码:

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

Also you can use a MessageBox with OKCancel options, but it requires many codes. 您也可以使用带有OKCancel选项的MessageBox ,但它需要许多代码。 The if block is for OK , the else block is for Cancel . if块用于OKelse块用于Cancel Here is the code: 这是代码:

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 MessageBox with YesNo options: 您还可以使用带有YesNo选项的MessageBox

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!");
}

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 eg MessageBoxButtons.YesNo),
(Enter the icon eg MessageBoxIcon.Question),
(Enter the default button eg MessageBoxDefaultButton.Button1)

More information can be found here 更多信息可以在这里找到

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'是一个'类型'但是像'变量'一样使用,即使使用:

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!");

In the System.Windows.Forms class, you can find more on the MSDN page for this here. System.Windows.Forms类中,您可以在此处的MSDN页面上找到更多信息。 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"); 由于您没有指定,如果您尝试在网页中执行此操作,则应该查看触发javascript alert("my message"); or confirm("my question"); confirm("my question"); functions. 功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM