C# Chapter 3
C# Chapter 3
Chapter 3
Message boxes, Dialog boxes and Menus
Menus
Menus consist of a menu bar that contains menus,
each of which drops down to display a list of
menu items.
Menu items are actually controls; they have properties and
events. Each menu item has a Name property, a Text
property, and a Click event, similar to a button.
To create menus for your application, you add a
MenuStrip component to a form.
Menus(Cont..)
The MenuStrip component appears in the component tray
below the form and the Menu Designer allows you to begin
typing the text for the menu items.
After typing the text for the first menu, you can add a
second menu or add menu items below the menu
name
Submenus
The drop-down list of items below a menu name is
called a menu item.
When an item on the menu has another list of
items that pops up, the new list is called a
submenu.
Submenus (Cont..)
Separator Bars
When you have many items in a menu, you should
group the items according to their purpose.
You can create a separator bar in a menu, which
draws a bar across the entire menu.
To create a separator bar, add a new menu item and click
on its drop-down arrow , Drop down the list and select
Separator.
Separator Bars (Cont…)
Context Menus
Context menus are the shortcut menus that pop up
when you right-click.
Generally, the items in a context menu are specific
to the component to which you are pointing,
listing the options available for that component or
that situation.
You assign the context menu to the form or
control by setting its ContextMenuStrip property.
Creating Context Menus
Add a ContextMenuStrip component to the
component tray and create the context menu using
the Menu Designer.
Defining Keyboard Access Keys
Many people prefer to use the keyboard, rather
than a mouse, for most operations.
Windows is set up so that most functions can be
done with either the keyboard or a mouse.
You can make your projects respond to the
keyboard by defining access keys , also called hot
keys .
Defining Keyboard Access Keys
You can set access keys for buttons, radio buttons,
and check boxes when you define their Text
properties.
Type an ampersand (&) in front of the character
you want for the access key.
Standards for Windows Menus Design
When you write applications that run under Windows,
your programs should follow the Windows standards.
You should always include keyboard access keys; if you
include keyboard shortcuts, such as Ctrl + key, stick
with the standard keys, such as Ctrl + P for printing.
Also, follow the Windows standards for placing the File
menu on the left end of the menu bar and ending the
menu with an Exit command.
If you have a Help menu, it belongs at the right end of
the menu bar
Common Dialog Boxes
You can use a set of predefined standard dialog
boxes in your projects for such tasks as specifying
colors and fonts, printing, opening, and saving.
Use the common dialog components in the
Dialogs tab of the toolbox to display the dialog
boxes that are provided as part of the Windows
environment.
The common dialog tools in the toolbox
Displaying a Windows Common Dialog Box
After you place a common dialog component on
your form, you can display the dialog box at run
time using the ShowDialog method .
Using the Color Dialog Box
The color selected by the user is stored in the
Color property.
You can assign this property to another object,
such as a control.
Displaying Messages in Message Boxes
You can display a message to the user in a
message box, which is a special type of window.
You can specify the message, an optional icon,
title bar text, and button(s) for the message box
Two sample message boxes created with
the Message Box.
The Message Box Object—General
Form
There is more than one way to call the Show
method of the Message Box class.
Each of the following statements is a valid call;
you can choose the format you want to use.
Message Box Buttons
When you show a message box, you can specify
the button(s) to display.
The choices are OK, OKCancel, RetryCancel,
YesNo, YesNoCancel, and AbortRetryIgnore.
The default for the Show method is OK, so unless
you specify otherwise, you will get only the OK
button in your message box.
Message Box Icons
Thank you