在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命名空間中,因此在使用它們之前,需要確保已經(jīng)引用了System.Windows.Forms程序集。