Perhaps create a language extension, in this case there are several to choose from.
In this case the following code is in a form, the source code is in a class project WindowFormsHelpersLibrary so to use them add a static using statement.
Note several also set the default button to No which I've found to be useful for those users who don't pay attention to say asking to delete a record for instance.
Form code
using static WindowFormsHelpersLibrary.LanguageExtensions.Dialogs;
using MessageBox = System.Windows.Forms.MessageBox;
if (MessageBox("Whatever to say", "Demo", MessageBoxIcon.Asterisk))
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("Cancelled");
}
if (MessageBox("Whatever to say"))
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("Cancelled");
}
if (Question("Continue with processing"))
{
MessageBox.Show("Ok");
}
else
{
MessageBox.Show("Cancelled");
}
InformationDialog("Operation completed","Bulk upload", MessageBoxIcon.Exclamation);
Source in class project
using System.Diagnostics;
using System.Windows.Forms;
using static System.Windows.Forms.MessageBox;
namespace WindowFormsHelpersLibrary.LanguageExtensions
{
public static class Dialogs
{
[DebuggerStepThrough]
public static bool Question(string text) =>
(Show(text, Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) == DialogResult.Yes);
[DebuggerStepThrough]
public static bool Question(string text, MessageBoxIcon icon) =>
(Show(text, Application.ProductName, MessageBoxButtons.YesNo, icon,
MessageBoxDefaultButton.Button2) == DialogResult.Yes);
[DebuggerStepThrough]
public static void InformationDialog(string message, string title) =>
Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
[DebuggerStepThrough]
public static void InformationDialog(string message, string title, MessageBoxIcon icon) =>
Show(message, title, MessageBoxButtons.OK, icon);
[DebuggerStepThrough]
public static void ErrorDialog(string message) =>
Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
[DebuggerStepThrough]
public static void ErrorDialog(string message, string title) =>
Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
[DebuggerStepThrough]
public static bool MessageBox(string text, MessageBoxIcon icon = MessageBoxIcon.Error) =>
Show(text, Application.ProductName, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
[DebuggerStepThrough]
public static bool MessageBox(string text,string title, MessageBoxIcon icon = MessageBoxIcon.Error) =>
Show(text, title, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
}
}
Or a simpler version
public static class Dialogs
{
[DebuggerStepThrough]
public static bool MsgBox(string text, MessageBoxIcon icon = MessageBoxIcon.Error) =>
MessageBox.Show(text, Application.ProductName, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
[DebuggerStepThrough]
public static bool MsgBox(string text, string title, MessageBoxIcon icon = MessageBoxIcon.Error) =>
MessageBox.Show(text, title, MessageBoxButtons.OKCancel, icon) == DialogResult.OK;
}
Usage
if (MsgBox("Continue?"))
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
if (MsgBox("Continue?", MessageBoxIcon.Question))
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}