Working with menus and dialog boxes
1 / 50

Working with Menus and Dialog Boxes - PowerPoint PPT Presentation

  • Uploaded on

Working with Menus and Dialog Boxes. Objectives. You will be able to Create and edit menus for Windows Forms applications. Write code to handle menu commands. Use a standard Open File dialog to permit a user to select a file to be opened. The Schedule Viewer Program.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Working with Menus and Dialog Boxes' - paul-moon

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript


You will be able to

  • Create and edit menus for Windows Forms applications.

  • Write code to handle menu commands.

  • Use a standard Open File dialog to permit a user to select a file to be opened.

The schedule viewer program
The Schedule Viewer Program

  • Existing program in Downloads area.


  • Reads a CSV file consisting of schedule entries and displays the schedule.

  • File path is hard coded.

    • Program opens the file on startup.

The schedule viewer program1
The Schedule Viewer Program

  • We will add a menu.

    • Traditional Windows menu.

    • Includes an Open command.

  • We will also add an Open File dialog

    • Permit the user to select the file to be viewed.

    • Traditional Windows Look and Feel


  • There is a zipped project folder and a data file in the Downloads area of the class web site:



  • Also, download the CSV schedule files.

  • Put the csv files into a convenient folder and set the path string in Form1.cs to match the Spring 2010.

  • Build and run the project

Tighten up some more columns
Tighten Up Some More Columns

cols[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

cols[3].Width = 30; // Credit Hours

cols[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

cols[6].Width = 30; // Seats Open

Menu conventions
Menu Conventions

  • Windows programs often have a menu bar.

  • Follow a pattern in their menus

    • File

      • New

      • Open

      • Save

      • Print

      • Exit

    • Edit

      • Copy

      • Paste

  • Users are familiar with this pattern.

  • How to create a menu
    How to Create a Menu

    • Design Time

      • Normal case

      • Menu Editor in Visual Studio .NET

    • Run Time

      • Program can build menu dynamically

    Creating a menu at design time
    Creating a Menu at Design Time

    • Open Form1 [Design]

    • View Toolbox

    • Expand “Menus & Toolbars” section

    • Drag the MenuStrip to the form.

    Add menustrip to form1
    Add MenuStrip to Form

    Menu component appears here (in “component tray”)

    The component tray
    The Component Tray

    • Represents components that do not otherwise provide a visible surface at design time.

    • Provides a way to access and set the properties of those components at design time.

    • Right click on control in component tray and select “Properties” to view and set its properties.

    Working with the menustrip

    Menu template appears here

    Type the menu heading here. (e.g., &File)

    Working with the MenuStrip

    Working with the menustrip1
    Working with the MenuStrip

    • A menu strip template appears at the top of the design surface.

      • If not, click on menuStrip1 in the component tray.

      • May need to expand the grey strip just below the title bar.

    • Type &File in the “Type Here” box

    • Type &Open in the next box that appears under the first one.

    Why “&”

    • The “&” in front of a letter in a menu caption defines a key to open that menu.

    • At runtime, the F in the menu caption will be underlined as a cue to the user

    • The user can type ALT+F to open the menu.

      • Same effect as clicking on the caption.

    • Holding down ALT, the user can type O to select the Open command.

      • Same effect as clicking on the command.

    Menu events
    Menu Events

    • There are a lot of menu events

      • See Events in Properties window.

      • You can write code to handle any of these events.

      • Normally you only need to be concerned with one of them: Click

    How to provide a click function
    How to Provide a “Click” Function

    • In designer mode, click on the menu caption. (e.g., File)

    • The menu drops down.

    • Double click on the menu item.

    • Visual Studio creates a stub for the Click function for that item and takes you to that point in the code window.

    Menu events1
    Menu Events

    Double click here.

    How to provide a click function1
    How to Provide a “Click” Function

    This appears in the editor window:

    private void openToolStripMenuItem_Click(object sender, EventArgs e)



    You normally won’t need to use these arguments.

    Add your code to handle the “Open” command here.

    Add open event handler
    Add Open Event Handler

    • Fill in a dummy event handler for now:

      MessageBox.Show("Open command");

    Build and start
    Build and start

    • Menu should appear.

    • Open should display message box.

    Add code to handle open command
    Add Code to Handle Open Command

    • Modify the program to open c:\schedule_2011_fall.csv and display the schedule when the user clicks Open

    • Move the call to import_schedule() and following statements from Form1_Load to openToolStripMenuItem_Click

      • replacing MessageBox.Show()

      • Try it!

    Adding commands to the menu
    Adding Commands to the Menu

    • Continue for as many commands as you need for that menu

    • Example:



    • To create a separator bar, type a minus sign as the command name.

    Adding commands to the menu1
    Adding Commands to the Menu

    • Continue to add command names and separator bars as needed.

    • Example:







    • Note that these are the conventional access keys for commands by these names.

    Setting menu item properties
    Setting Menu Item Properties

    • Right click on the menu component in the component tray

    • Select “Properties” on the pop up menu.

    Setting menustrip properties
    Setting MenuStrip Properties

    • You can change properties of the overall MenuStrip in its Properties window.

    • Example: RightToLeft supports languages that write from right to left.

      • You won’t need to think about this so long as you only use English in your application.

      • FYI – Microsoft provides extensive support for “internationalization”.

    Setting menu item properties1
    Setting Menu Item Properties

    • Each item on the menu has its own properties.

    • Right click on the item to open a context menu, then click on Properties.

    Setting properies of open

    Right click here.

    Then click here.

    Setting Properies of Open

    Properties window for the menu item appears.

    Shortcut keys
    Shortcut Keys

    • Shortcut Keys vs. Access Keys

      • Access Keys, like Alt+X, only work when the menu is dropped down.

        • When you could click the command

      • A “shortcut” key can be used at any time

        • Example: Ctrl+S for Save

      • You can use the Properties window to define shortcut keys.

        • Ctrl+O for Open

    Shortcut keys1
    Shortcut Keys shortcut key.

    Shortcut keys2
    Shortcut Keys shortcut key.

    • At runtime the menu will indicate the shortcut key in the form Ctrl+O

    How to disable a menu item
    How to Disable a Menu Item shortcut key.

    • Sometimes a command doesn’t make sense.

      • e.g. “Print” when there is nothing to print

    • Rather than giving an error message, make the command unavailable.

    • You could remove the item from the menu,

      • or you could just disable it.

    • Set the Enabled property to false.

    Exit command
    Exit Command shortcut key.

    • Let’s add an Exit command to the File menu.

    • If you have not already done so,

      • Type – in command name box to create a separator.

      • Type E&xit for the Exit command.

    Adding the exit command
    Adding the Exit Command shortcut key.

    Double click on Exit command to add event handler.

    Example exit command
    Example: Exit Command shortcut key.

    private void exitToolStripMenuItem_Click(

    object sender, EventArgs e)




    Try it!

    End of Section

    Common dialog controls
    Common Dialog Controls shortcut key.

    • Permit the user to say where to open or save a file by navigating to it.

    • Let’s add an Open File dialog for our Open command.

      • Replace hard coded file path.

    Adding an openfiledialog
    Adding an OpenFileDialog shortcut key.

    • View > Toolbox

    • Expand Dialogs section

    • Drag OpenFileDialog to form

    Adding an openfiledialog2
    Adding an OpenFileDialog shortcut key.

    openFileDialog1 appears in the component tray.

    Add to import schedule
    Add to import_schedule shortcut key.

    void import_schedule()


    StreamReader Reader = null;

    String input_line;

    String file_name;

    Schedule = new List<Schedule_Record>();

    file_name = @"C:\schedule_2011_spring.csv";

    openFileDialog1.FileName = file_name;

    DialogResult result = openFileDialog1.ShowDialog();

    if (result == DialogResult.OK)


    file_name = openFileDialog1.FileName;


    New open file dialog
    New Open File Dialog shortcut key.

    Summary shortcut key.

    • Adding a menu bar to a Windows program is easy.

    • Menu commands work essentially just like buttons.

    • Common File Dialogs permit uses to select files in the customary manner.