Fundamentals of Message Boxes

Introduction

A message box is a special dialog box used to display a piece of information to the user. As opposed to a regular form, the user cannot type anything in the dialog box. There are various ways to get a message box. You primarily have three options: the .NET Framework, the Win32 library, and other libraries such as the Visual Basic library.

Practical LearningPractical Learning: Starting a Project

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project wizard page, in the list of projects templates, click Windows Forms App
  4. Click Next
  5. In the Configure Your New Project wizard page, change the Project Name to Exercise7
    Accept or change the project Location
  6. Click Next
  7. In the Framework combo box, select the highest version (.NET 9.0 (Standard Term Support)).
    Click Create
  8. To execute, on the main menu, click Debug -> Start Without Debugging:
  9. Close the form and return to your programming environment
Author Note

Author Note

If you want, using the form of the application you created, you can apply the descriptions in the following sections to experiment with, and get, the same results.

The .NET Message Boxes

Probably the easiest way to get a message box is by using the .NET Framework. In fact, to support message boxes, the .NET Framework provides a static class named MessageBox.

The Return Value of a Message Box

The primary purpose of a message box is to display a message. Besides displaying a message, a message box may let the user make a decision by clicking a button. Depending on the button the user would have clicked, the message box would return a value. The value returned by a message box corresponds to the particular button the user would have clicked (on the message box). The return values of a message box are defined in an enumeration named DialogResult. The buttons and the returned values are as follows:

If the User Clicks The method Returns
Abort DialogResult.Abort
Cancel DialogResult.Cancel
Ignore DialogResult.Ignore
No DialogResult.No
OK DialogResult.OK
Retry DialogResult.Retry
Yes DialogResult.Yes

The Message of a Message Box

The .NET Framework provides the MessageBox static class to easily create a message box. To display a simple message with just an OK button, you can call its static Show() method. Its syntax is as follows:

public static DialogResult MessageBox.Show(string message);

In this case, the message to display must be passed as a string to the Show() method. Here is an example:

using System.Windows.Forms;

public class Exercise
{
    static int Main()
    {
        MessageBox.Show("Welcome to the Wonderful World of C# programming");
        return 0;
    }
}

This would produce:

Message Box

The message to display can be made of up to 1024 characters. To display the message on multiple lines, you can use the new line escape sequence anywhere inside the string.

The Primary Characteristics of a Message Box

The Caption of a Message Box

The MessagBox.Show() method is overloaded with various versions. Another version uses the following syntax:

public static DialogResult Show(string text, string caption);

This version allows you to specify a custom caption for the message box. With this version, the first argument is the string that the user will see displaying on the message box. You can pass it as a string. You can also create it from other pieces of strings.

The second argument, caption, will be the sentence to display in the title bar of the message box. Here is an example:

using System.Windows.Forms;

public class Exercise
{
    static void Main()
    {
        MessageBox.Show("Welcome to the wonderful world of C# programming", "Exercise");
    }
}

This would produce:

The Buttons of a Message Box

Another version of the MessageBox.Show() method uses the following syntax:

public static DialogResult Show(string text, string caption, MessageBoxButtons buttons);

This version allows you to display one or more buttons on the message box. The available buttons are defined through an enumeration named MessageBoxButtons. In the Visual Basic language, the buttons of the message box are controlled by an enumeration named MsgBoxStyle. In the Visual Basic language, the buttons of the message box are controlled by an enumeration named MsgBoxStyle. The members of these enumerations are:

MessageBoxButtons MsgBoxStyle Message Box Buttons
OK OKOnly 0 OK
OKCancel OKCancel 1 OKCancel
YesNo YesNo 4 Yes
YesNoCancel YesNoCancel 3 Yes No Cancel
RetryCancel RetryCancel 5 Retry Cancel
AbortRetryIgnore AbortRetryIgnore 2 Abort Retry Ignore

To use any of these combinations of buttons, call the MessageBoxButtons enumeration and access the desired combination. Here is an example:

using System.Windows.Forms;

public class Exercise
{
    static int Main()
    {
        MessageBox.Show("Welcome to the wonderful world of C# programming",
                        "Exercise",
                        MessageBoxButtons.OKCancel);
        return 0;
    }
}

This would produce:

Message Box With Buttons

The Icon of a Message Box

This version allows you to display an icon. The possible icons are available through the MessageBoxIcon enumeration. The members of this enumerator are:

MessageBoxIcon Description
None    
Asterisk Asterisk Asterisk
Error Critical Error
Exclamation Exclamation Exclamation
Hand Critical Hand
Information Information Information
Question Question Question
Stop Critical Stop
Warning Exclamation Warning

Here is an example:

public class Exercise
{
    static void Main()
    {
        MessageBox.Show("Your order appears to be correct" +
                        "\nAre you ready to provide your credit card information?",
                        "Customer Order Processing",
                                              MessageBoxButtons.YesNoCancel,
                                              MessageBoxIcon.Information);
    }
}

This would produce:

Message Box With an Icon

The Default Button of a Message Box

When a message box is configured to display more than one button, the operating system is set to decide which button is the default. The default button has a thick border that sets it apart from the other button(s). If the user presses Enter, the message box would behave as if the user had clicked the default button. If the message box has more than one button, you can decide what button would be the default. To specify the default button, the MessageBox.Show() method provides the following version:

public static DialogResult Show(string text,
                                string caption,
                                MessageBoxButtons buttons,
                                MessageBoxIcon icon,
                                MessageBoxDefaultButton defaultButton);

Based on this, you can specify the default button using the last argument that provides values through the MessageBoxDefaultButton enumerator whose values are:

Button1: The left button will be the default. Here is an example:

public class Exercise
{
    static void Main()
    {
	    MessageBox.Show("Your order appears to be correct" +
                        "\nAre you ready to provide your credit card information?",
		                "Customer Order Processing",
                        MessageBoxButtons.YesNoCancel,
			            MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button1);
    }
}

This would produce:

Message Box With a Defauft Button

Button2: If the message box displays two buttons, the right button will be the default. If the message box displays three buttons, the middle button will be the default. Here is an example:

public class Exercise
{
    static void Main()
    {
       	MessageBox.Show("Your order appears to be correct" +
                        "\nAre you ready to provide your credit card information?",
	                    "Customer Order Processing",
                        MessageBoxButtons.YesNoCancel,
			            MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button2);
    }
}

This would produce:

Message Box With a Default Button

Button3: The right button will be the default. Here is an example:

public class Exercise
{
    static int Main()
    {
       	MessageBox.Show("Your order appears to be correct" +
                        "\nAre you ready to provide your credit card information?",
	                    "Customer Order Processing",
                        MessageBoxButtons.YesNoCancel,
			            MessageBoxIcon.Information,
                        MessageBoxDefaultButton.Button3);
    }
}

This would produce:

Message Box With a Default Button

Options on Creatin a Message Box

Introduction

The Visual Basic Message Boxes

Introduction

The Visual Basic language, through its own library, provides a good and easy way to create a message box.

If you want to use the Visual Basic language to display a message box, first add a reference of the Microsoft.VisualBasic.dll to your project (to the References node of the Solution Explorer). To support message boxes, the Visual Basic language provides a function named MsgBox that is a member of the Interaction static class.

Returning a Value from a Message Box

Remember that a message box can be used to ask a question to a user who would answer by clicking a button. To support the answer of a question, the Interaction.MsgBox() function of the Visual Basic language returns a member of an enumeration named MsgBoxResult.

The Message of a Message Box

Here is the Visual Basic version of the above code:

using Microsoft.VisualBasic;

public class Exercise
{
    static void Main()
    {
        Interaction.MsgBox("Welcome to the wonderful world of C# programming.");
    }
}

The Caption of a Message Box

The MessagBox.Show() :



                

The Visual Basic version of the above code is:

using Microsoft.VisualBasic;

public class Exercise
{
    static void Main()
    {
        Interaction.MsgBox("Welcome to the wonderful world of C# programming.", 0, "Exercise");
    }
}

The Win32 Message Boxes

Introduction

The primary way the Microsoft Windows operating systems support message boxes is through its own Win32 library.

ApplicationPractical Learning: Ending the Lesson


Previous Copyright © 2001-2025, FunctionX Thursday 08 May 2025, 08:13 Next