Messgaebox C#
Messgaebox C#
Messgaebox C#
Show
Dialog boxes interrupt users.
They force users to respond
before further action is taken.
This is necessary in some
situations.MessageBox.Show
is useful if a warning is
important or a serious error
occurred.
Example
To start, the
MessageBox.Show method is a
static method, which means
you do not need to create a
new MessageBox() anywhere
in your code. Instead, you can
simply type "MessageBox" and
press the period, and then
select Show.
Static Method
In this example, the
MessageBox.Show method is
used in the Form1_Load event
handler. To make the
Form1_Load event handler,
create a new Windows Forms
application and double-click on
the window in the designer.
Windows Forms program that uses MessageBox: C#
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//
// The simplest overload of MessageBox.Show. [1]
//
MessageBox.Show("Dot Net Perls is awesome.");
//
// Dialog box with text and a title. [2]
//
MessageBox.Show("Dot Net Perls is awesome.",
"Important Message");
//
// Dialog box with two buttons: yes and no. [3]
//
DialogResult result1 = MessageBox.Show("Is Dot Net
Perls awesome?",
"Important Question",
MessageBoxButtons.YesNo);
//
// Dialog box with question icon. [4]
//
DialogResult result2 = MessageBox.Show("Is Dot Net
Perls awesome?",
"Important Query",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
//
// Dialog box with question icon and default button.
[5]
//
DialogResult result3 = MessageBox.Show("Is Visual
Basic awesome?",
"The Question",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
//
// Test the results of the previous three dialogs.
[6]
//
if (result1 == DialogResult.Yes &&
result2 == DialogResult.Yes &&
result3 == DialogResult.No)
{
MessageBox.Show("You answered yes, yes and no.");
}
//
// Dialog box that is right-aligned (not useful).
[7]
//
MessageBox.Show("Dot Net Perls is the best.",
"Critical Warning",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Warning,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign,
true);
//
// Dialog box with exclamation icon. [8]
//
MessageBox.Show("Dot Net Perls is super.",
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}
}
Overload
Typing in the options. The
easiest way to use
MessageBox.Show is to type in
"MessageBox", and then press
period, and then select Show.
Next, Visual Studio shows a
popup with the overload list.
You can scroll through the
overload lists.
Tip:For a parameter such
as "MessageBoxButtons",
type in
"MessageBoxButtons" and
press period to see all the
options.
Also:You do not need to
create a new
MessageBoxButtons()
object. This is an enum
type, not a class.
Enum
The order of the parameters
in the MessageBox.Show
method calls is important. The
parameter order gives the
compiler the ability to apply
DialogResult
In Windows Forms,
DialogResult is not an actual
class but is a named constant
from an enumeration. This
means you cannot create a
new DialogResult with the
"new" operator. First assign
your variable to the result of
MessageBox.Show.
Overloads
User experience
Summary
MessageBox.Show is an
effective approach to dialog
boxes in Windows Forms. We
looked at screenshots of the
results of the
MessageBox.Show method. We
also tested the DialogResult
enumeration.
And:There are many
options here. We can
choose between many
parameters to the static
method.
So:The
MessageBox.Show
method is ideal for
many simpler Windows
Forms programs with
less specific
requirements.