CH 05

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CIS 3260 – Intro to Programming with C#

Menus and Common


Dialog Boxes

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved.

 Create menus and submenus for program


control
 Display and use the Windows common dialog
boxes

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-2

 Menu Bar
◦ Menus that drop down to display a list of menu
items
 Menu items are controls with properties and events
 Each menu item has a Name and Text property and a
Click event (similar to a button)
 Easy to create menus for a Windows form
using Visual Studio's Menu Designer
◦ Menus look and behave like standard Windows
menus

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-3

Dr. David McDonald Chapter 5 – Page 1


CIS 3260 – Intro to Programming with C#

 Group menu items


according to their
purpose
 Draws a bar across
the entire menu
 To create a separator
bar, add a new menu
item and click on its
drop-down arrow
 Select Separator
or type a single
hyphen in the Menu
Designer
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-4

 Menu items have


Enabled property set
to true by default
 Enabled menu items
appear in black text
and can be selected
 Disabled items are
not available
 Enabled property can
be set at design time
or run time, in code
instructionsToolStripMenuItem.Enabled = false;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-5

 Menu items have


Checked property
set to false by
default
 A check mark next
to a menu item
indicates the option
is selected
 Checked property
can be set at design
time or in code summaryToolStripMenuItem.Checked = true;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-6

Dr. David McDonald Chapter 5 – Page 2


CIS 3260 – Intro to Programming with C#

 Set a menu item’s initial Checked property


in the Properties window
 To change the menu item’s state in code,
set its Checked property to true or false
if (summaryToolStripMenuItem.Checked)
{
// Uncheck the summary menu item.
summaryToolStripMenuItem.Checked = false;
}
else
{
//Check the summary menu item.
summaryToolStripMenuItem.Checked = true;
}

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-7

 Many users prefer to use keyboard shortcuts


for selecting menu items
 To set keyboard shortcuts
◦ Select the menu item in the designer
◦ In the Properties window, select the ShortcutKeys
property
◦ Make a choice from the drop-down list
◦ Set ShowShortcutKeys property to false to prevent a
shortcut from showing on the menu

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-8

 Predefined standard dialog boxes


◦ Specifying colors and fonts
◦ Opening, saving and browsing for files
 Place the desired common dialog component
from the Dialogs tab of the toolbox in the
Component Tray
◦ Default names for the components are fine

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-9

Dr. David McDonald Chapter 5 – Page 3


CIS 3260 – Intro to Programming with C#

 Display the dialog box at run time using the


ShowDialog method
 The dialogObject is the name of the common
dialog component
 Write code to show the dialog in the event
handler for a menu item or button

colorDialog1.ShowDialog();
fontDialog1.ShowDialog();

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-10

 A dialog box is said to be modal


◦ The box stays on top of the application and must
be responded to
◦ Use the ShowDialog method to display a dialog box,
which is a window displayed modally
 A window that does not require response is
said to be modeless
◦ Use the Show method to display a modeless window

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-11

 Color selected in the Color dialog box is


stored in the Color property
 Color property can be assigned to another
object, such as a control
◦ Display dialog box with ShowDialog method
◦ User responds to dialog box
◦ Use the Color property
colorDialog1.ShowDialog();
titleLabel.ForeColor = colorDialog1.Color;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-12

Dr. David McDonald Chapter 5 – Page 4


CIS 3260 – Intro to Programming with C#

 Color selected in the Color dialog box is


stored in the Color property
 Color property can be assigned to another
object, such as a control
◦ Display dialog box with ShowDialog method
◦ User responds to dialog box
◦ Use the Color property
colorDialog1.ShowDialog();
titleLabel.ForeColor = colorDialog1.Color;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-13

 The Font common


dialog box
displays available
fonts for the
system display
 After the user
makes a selection,
the Font property
can be assigned to
other objects on fontDialog1.ShowDialog();
the form titleLabel.Font = fontDialog1.Font;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-14

 Before executing the ShowDialog method,


assign the existing values of the object's
properties
 When the dialog box appears, the current
values will be selected
 If the user presses Cancel, property setting
for the objects will remain unchanged

colorDialog1.Color = titleLabel.ForeColor; fontDialog1.Font = titleLabel.Font;


colorDialog1.ShowDialog(); fontDialog1.ShowDialog();
titleLabel.ForeColor = colorDialog1.Color; titleLabel.Font = fontDialog1.Font;

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. 5-15

Dr. David McDonald Chapter 5 – Page 5

You might also like