Site Search

Lab 4: Menus

Learning Objectives for Lab 4

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

.NET Predefined Dialog Boxes

Predefined Dialog Boxes

The .NET framework provides a number of predefined dialog box classes which you can adapt for your own applications. Rather than writing your own Open File Dialog or Page Setup Dialog, for example, you can use the predefined classes and methods in your program to do the work.

You then write the code to work with the Windows Forms Dialog Boxes

Activity: Create a New Project called WindowsUI in Solution Lab4

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

Build the MainForm

Main Form

Activity: Add Controls to the MainForm

 private void openFileButton_Click(object sender, EventArgs e)
 {
   DialogResult result = openFileDialog1.ShowDialog();

   if (result == DialogResult.OK)
   {
      textBox1.Text = openFileDialog1.FileName;
   }
 }

The beauty of I/O streams

One of the greatest inventions since sliced bread is I/O streams. We can easily read in text from a file using the .NET StreamReader class

   if (result == DialogResult.OK)
   {
      System.IO.StreamReader reader = new System.IO.StreamReader(openFileDialog1.FileName);
      this.textBox1.Text = reader.ReadToEnd();
      reader.Close();
   }

Activity 3: Display the contents of the file in textBox1

Note: Being careful programmers, we should use a try/catch/finally block for any I/O, placing the Close method call in a finally block.

    System.IO.StreamReader reader = null;
    try 
    {
      reader = new StreamReader(openFileDialog1.FileName);
      this.textBox1.Text = reader.ReadToEnd();
    }
    catch (Exception ex) 
    {
      MessageBox.Show("File Open Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally 
    {
      if (reader != null)
        reader.Close();
    }

Drop Down Menus

Drop Down Menus

Many modern GUI applications use some version of menus: drop-down menus and context menus (pop-up menus accessible by right-click an object) being the most common.

Activity: Add a main menu to your form

Note: by default, access keys will not show up as underlined. Press the alt key to see them show up. To change the default behavior under Windows XP, right-click the desktop and select properties. Then select Appearance tab > Effects button. Un-check the box "Hide underline letters"

The MenuStrip's smart task can be used to Insert Standard Items

Activity: Add an ExitToolStripMenuItem event handler to end the application

      private void exitToolStripMenuItem_Click(object sender, EventArgs e)
      {
         this.Close();
      }

Activity: Call the openFileButton_Click event handler if the user selects the File > Open menu item

There is a lot of functionality provided with menus, and should you decide to use them in your CS 446 project, take some time on your own to study up on the options

Context Menus

context menus

Context Menus provide users with an easily accessible menu (usually by right-click) of frequently used commands associated with the selected object.

The items in a context menu are often a subset of the items from main menus that appear elsewhere in the application.

TextBoxes come with a basic functional context menu built in.

Toolbars

Toolbar example

Toolbars are built-in .NET controls often used to display small buttons that provide easy and fast access to frequently used commands. Modern toolbars are not limited to the display of just buttons however. In addition, they may include drop downs, combo boxes, and other advanced controls.

Essentially, a Toolbar control consists of an ImageList object and a collection of buttons.

Visual Studio .NET makes creating toolbars for your application very easy.

Activity: Add an ToolBar to your MainForm

The ToolStripContainer control that provides panels along each of the four sides of a form to contain menus, toolbars, and status bars.

Handling Toolbar events

The user clicking on a toolbar button causes a toolStripButton Click event. Normally we will want to re-use our menu click event handler with our tool bar buttons

URL: http://www/cwu.edu /~gellenbe/446/labs/lab4.php
Author: Ed Gellenbeck, Department of Computer Science, Central Washington University, gellenbe@cwu.edu
Copyright 2006 Ed Gellenbeck, Central Washington University
Last modified: April 07, 2008