Senin, 19 Agustus 2013

MessageBox Class in Windows Form

MessageBox class is used to display informative messages to users. A message box can contains text, buttons and symbols. One cannot create a new instance of this class something like other classes. To display a message you need to call show() method of this class. It is a static method and accepts text, title, buttons, icons and etc. as per desired parameters.

To show a simple message to the user:
MessageBox.Show("This is My First Message");

And it will provide a message box as in following image

Message Box with text only in windows forms
Show method have some overloads, we will use a standard overload that is mostly used in programming.

MessageBox.Show("This is My First Message","Title",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information);

A message box will show like the following image.

Message Box with text, title, buttons and icons in windows forms

In the above show method the first parameter is text to be shown, second parameter is the captions or title of the box, third parameter is the messageBox buttons (here OkCancel) and fourth parameter is the icon(here information icon) to be shown. There are some more buttons and icons to be used in this method which are:

MessageBox buttons which are in enumeration MessageBoxButtons
  • AbortRetryIgnore: contains Abort, Retry and Ignore button.
  • Ok: contains ok button.
  • OkCancel: contains Ok and Cancel button.
  • RetryCancel: contains Retry and Cancel button.
  • YesNo: contain Yes and No buttons.
  • YesNoCancel: contains Yes, No and Cancel buttons.
Message box icons which are in enumeration MessageBoxIcons
  • Asterisk: symbol consisting a letter ‘l’ within a circle.
  • Error: symbol consisting a letter ‘X’ within a white circle and red background.
  • Exclamation: symbol consisting an exclamation mark within a rectangle.
  • Hand: symbol consisting a white ‘X’ within a circle.
  • Information: symbol consisting a letter ‘i’ within a circle.
  • None: contains no symbol.
  • Question: symbol consisting a question mark within a circle.
  • Stop: symbol consisting a white X within a circle.
  • Warning: symbol consisting a exclamation point within a triangle.
Here are some more overloads of Show() method.
MessageBox.Show("Text");
MessageBox.Show("Text", "Title");
MessageBox.Show("Text", "Title", MessageBoxButtons.OK);
MessageBox.Show("This is My First Message", "Title", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
We can set default button of the box, enable options of message box, enable of disable help button and etc.as per our requirements.

Status Strip Control
Error Provider Control

Tidak ada komentar:

Posting Komentar