ARTICLE

MessageBox in Windows Form in C#

Posted by Muhammad Irfan Articles | Windows Forms C# May 12, 2010
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.
Reader Level:
Download Files:
 

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:

  1. Write your condition in message text.
  2. Add any Message Box Button you want (Ok, Ok/Cancel, Yes/No, Yes/No/Cancel, etc.)
  3. 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
      }
    }

COMMENT USING