Reader Level:
Articles

MessageBox in Windows Form in C#

By Muhammad Irfan on 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.
  • 0
  • 0
  • 24570
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
      }
    }

Muhammad Irfan

A simple, creative, curious, nature lover, not so much naughty, funny & friendly person with a zest for life, my friends call me Irfan(?????). Like nature, creativity, reading newspapers & journals cartoons, hist... Read more

COMMENT USING

Trending up