在C#中,MessageBox类提供了多种显示消息框的方法,以下是一些常用的方法:
MessageBox.Show("Hello, World!");
DialogResult result = MessageBox.ShowDialog("Are you sure you want to exit?");
if (result == DialogResult.OK)
{
// User clicked OK
}
else
{
// User clicked Cancel or closed the dialog
}
MessageBox.ShowInformation("Important Information", "Please read and accept the terms.");
MessageBox.ShowWarning("Attention Required", "Please be cautious when proceeding.");
MessageBox.ShowError("An Error Occurred", "An unexpected error occurred. Please try again later.");
DialogResult result = MessageBox.ShowConfirmation("Are you sure you want to proceed?", "Confirmation Required");
if (result == DialogResult.Yes)
{
// User clicked Yes
}
else
{
// User clicked No or closed the dialog
}
注意:这些方法都位于System.Windows.Forms命名空间中,因此在使用它们之前,需要确保已经引用了System.Windows.Forms程序集。