Introduction:
In this article, we will learn how to display a simple and conditional
MessageBox in Windows Form in C#. It is very easy and simple.
Description:
Simple Message Box:
Write MessageBox.Show("Text") in any control code (Button etc.) and write your
message like:
private void Simple_button_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello MessageBox.....");
}
You can add message header, button and message icon in the above code like:
private void Simple_button_Click(object sender, EventArgs e){
MessageBox.Show("Welcome to Simple MessageBox", "Simple",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Conditional Message Box:
Conditional message box is same like a simple but with the difference of writing
the code and write your code like: - Write your condition in message text.
- Add any Message Box Button you want (Ok, Ok/Cancel, Yes/No, Yes/No/Cancel,
etc.)
- Write the following code to present conditional message box in the control
code:
private void Conditional_button_Click(object sender, EventArgs e) {
DialogResult = MessageBox.Show("Welcome to Conditional MessageBox \n
Please select one option Yes/No)",
"Conditional", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);if (DialogResult == DialogResult.Yes)
{
// any function
}
else
{
// any function
}
}