如何在.NETWinforms 中显示自定义错误或警告消息框?

我怎样才能显示一个“叮”的消息框声音和一个红色的’关闭’按钮在里面?这就是我说的:

screenshot

我试图创建一些自定义错误和警告,但是:

MessageBox.Show("asdf");

似乎没有给我任何定制选项。

402717 次浏览

Try this:

MessageBox.Show("Some text", "Some title",
MessageBoxButtons.OK, MessageBoxIcon.Error);

Try details: use any option:

MessageBox.Show(
"your message",
"window title",
MessageBoxButtons.OK,
MessageBoxIcon.Warning // for Warning
//MessageBoxIcon.Error // for Error
//MessageBoxIcon.Information  // for Information
//MessageBoxIcon.Question // for Question
);
MessageBox.Show(
"your message",
"window title",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk //For Info Asterisk
MessageBoxIcon.Exclamation //For triangle Warning
)

You should add namespace if you are not using it:

System.Windows.Forms.MessageBox.Show("Some text", "Some title",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);

Alternatively, you can add at the begining of your file:

using System.Windows.Forms

and then use (as stated in previous answers):

MessageBox.Show("Some text", "Some title",
MessageBoxButtons.OK, MessageBoxIcon.Error);