Site Search

Lab 3: Dialog Boxes

Learning Objectives for Lab 3

After completion of today's lab, you should be able to

In addition, an introduction to the Program 1 assignment will be covered.

Message Boxes

Message Boxes are secondary windows used to interact with a user outside a primary window. They are commonly used to present brief messages or to request confirmation for actions.

Confirmation takes input from command buttons.

Information Message Box    Sample Message Box    Yes No Message Box

MessageBox Class

A MessageBox is a predefined dialog box that displays application-related information to the user. Message boxes are also used to request information from the user.

Message boxes are used quite often to display "error messages" to a user

   MessageBox.Show("You must enter your name",   //message displayed
        "Microsoft Word",                         //caption in title bar of window
        MessageBoxButtons.OK,                     //type of buttons included
        MessageBoxIcon.Exclamation);              //type of icon displayed

There are six enumerated types of MessageBoxButtons defined: AbortRetryIgnore, OK, OKCancel, RetryCancel, YesNo, and YesNoCancel

There are nine enumeration types of MessageBoxIcons: Asterisk, Error, Exclamation, Hand, Information, None, Question, Stop, Warning, and None (although several of them use the same icon)

Activity 1: Create a New Project

Create a New Project called WindowsUI in Solution Lab3

  1. From the File menu, choose New > Project
  2. From the New Project dialog box, choose Visual C# Windows Application
  3. Enter WindowsUI as the project name, U:\Labs as the Location, and Lab3 as the solution name.
  4. Check Create directory for solution
  5. Click OK
  6. Use Visual Studio 2008's refactoring tool to change the name of the file Form1.cs object to MainForm.cs
  7. Change the text displayed in MainForm's title bar to Lab3

Activity 2: Create an About Box MessageBox

Main Form    About Message Box

Create an About Box MessageBox

Add an About Button to your MainForm

  1. Add an "About" Button to the MainForm. Set its
    • Name property to aboutButton
    • Text property to "About"
    • Use the Format menu to center it form horizontally in the form
  2. Add a Click Event Handler method for the aboutButton
    • Create a Button Click event
    • Write the C# code to show an Information MessageBox with your name as the author
  3. Run your application to test you MessageBox

If you finish early, experiment with the other

Getting user responses through Message Boxes

Message boxes are also frequently used to get simple yes/no responses from the user

     DialogResult result = MessageBox.Show("Do you really want to erase your hard disk?", 
                                               "Norton Utility", 
                                                MessageBoxButtons.YesNoCancel, 
                                                MessageBoxIcon.Question);

     if (result == DialogResult.Yes)
        MessageBox.Show("Hard Disk Erased");

Where DialogResult has eight enumerated values: None, OK, Cancel, Abort, Retry, Ignore, Yes, and No

Custom Dialog Boxes

Dialog Boxes are secondary windows used to interact with a user outside a primary window. .NET includes a number of pre-defined dialog boxes (e.g. Open, Save As, Print) that we can use in our Windows applications.

Creating your own custom dialog box is done through the declaration of a user-defined class derived from System.WindowsForms.Form class, instantiating an object of that type in a primary window, and then calling its Show method.

A typical use of a custom dialog box is to collect user input.

When creating dialog boxes, the following traits are typically set:

`

Activity 3: Create a Custom Dialog Box

About Dialog Box

Add a custom dialog box to your Windows Application

  1. From the Project Menu, Add Windows Form ... named AboutForm
    • Set its FormBorderStyle property to FixedDialog
    • Set its MaximizeBox and MinimizeBox properties to False
    • Set its ShowInTaskbar property to False
  2. Drag and Drop a Label to AboutForm
    • Use your own name as its Text property
  3. Drag and Drop a Button to your AboutForm
    • Set its Name property to okButton
    • set its Text property to "OK"
    • set its DialogResult property to OK

Call your custom dialog box from your primary window

      private void aboutButton_Click(object sender, EventArgs e)
      {
         AboutForm myAboutBox = new AboutForm();
         myAboutBox.ShowDialog();
      }

Pretty simple, huh? Almost all applications need an About Dialog accessible from the Help menu. See SSW's guidelines for an effective About Dialog.

Visual Studio 2008 has a pre-defined About Box form you can use in your application.

Demo: add a new About Box item to your application and view its controls with the Document Outline view

Modal versus Modeless

There are two types of dialog boxes: modeless and modal.

Most dialog boxes are set up as modal to constrain the user what the user can do next. Many user interface gurus claim that modeless is preferred in many cases. Modeless keeps the user in control of the actions, rather than forced to finish or cancel the dialog box.

To display a dialog box as modeless, use the Show( ) method in place of the ShowDialog( ) method.

You will also need to explicitly handle the button clicks on modeless form.

   private void okButton_Click(object sender, System.EventArgs e) 
   {
      //Do any processing required in modeless dialog box
      this.Close();
   }

Dialog Boxes for user input

A common technique is to create a modal dialog box to obtain input from the user.

As a simple example, let us create a dialog box so that users can enter their personal information. To keep things easy, we will only have them enter their name and age.

Activity 4: Add a Class to store user-input

Add a C# Code Class to your project named Customer

  1. From the Project Menu, Select Add Class, select Code, and select Class. Name your file Customer.cs
  2. Change Customer class accessibility to public (from none)
  3. Copy and Paste the C# code for the Customer class
public class Customer
{
   //instance variables
   private string name;
   private int age;

   //constructor
   public Customer()
   {
      name = "";
      age = 0;
   }

   //Name property
   public string Name
   {
      get { return name; }
      set { name = value; }
   }

   //Age property
   public int Age
   {
      get { return age; }
      set { age = value; }
   }
}

Activity 5: Using Dialog Boxes to Collect User Input

User Input Form

Add a custom dialog box to collect user input

Modify the code in UserInputForm class in to

   public partial class UserInputForm : Form
   {
      //instance variable
      private Customer user;

      //constructor method
      public UserInputForm(Customer cust)
      {
         InitializeComponent();
         user = cust;
      }

      //Save the user's name and age
      private void okButton_Click(object sender, EventArgs e)
      {
         user.Name = nameTextBox.Text;
         user.Age = Int32.Parse(ageTextBox.Text);
      }
   }

Note: Since this is a prototype, error checking of input is ignored

Add Customer Button

Modify the MainForm to call the UserInputForm

      private void addCustomerButton_Click(object sender, EventArgs e)
      {
         //declare and instantiate a new customer
         Customer newCustomer = new Customer();

         //instantiate and call the dialog box
         UserInputForm userData = new UserInputForm(newCustomer);
         DialogResult result = userData.ShowDialog();

         //display the new customer data entered in the dialog box
         if (result == DialogResult.OK)
         {
            MessageBox.Show(String.Format(
               "Name is {0}\nAge is {1}", newCustomer.Name, newCustomer.Age),
               "New User",
               MessageBoxButtons.OK,
               MessageBoxIcon.Information);
         }
      }

Program 1

Introduction to Program 1 assignment