Creating Own MessageBox in Windows Form Application:
*Creating Own MessageBox Control
To create our own MessageBox control simply follow the guide below…
namespace MsgBoxExample {
public partial class MsgBoxExampleForm : Form {
//Constructor, called when the class is initialised.
public MsgBoxExampleForm() {
InitializeComponent();
}
//Called whenever the button is clicked.
private void btnShowMessageBox_Click(object sender, EventArgs e) {
CustomMsgBox.Show($"I'm a {nameof(CustomMsgBox)}!", "MSG", "OK");
}
}
}
private DialogResult result = DialogResult.No;
public static DialogResult Show(string text, string caption, string btnOkText) {
var msgBox = new CustomMsgBox();
msgBox.lblText.Text = text; //The text for the label...
msgBox.Text = caption; //Title of form
msgBox.btnOk.Text = btnOkText; //Text on the button
//This method is blocking, and will only return once the user
//clicks ok or closes the form.
msgBox.ShowDialog();
return result;
}
private void btnOk_Click(object sender, EventArgs e) {
result = DialogResult.Yes;
MsgBox.Close();
}