62
LESSON 5 Making Menus
2. Add a TextBox to the form. Type some text into its Text property and set its other
properties:
Name = txtContents, MultiLine = True, Dock = Fill, ScrollBars = Both.
3. Create the context menu by double-clicking the Toolbox’s ContextMenuStrip tool.
Create the main menu structure.
1. Select the MenuStrip. Click the Type Here box and type &File.
2. In the Type Here box below the File menu, type E&xit.
By convention, the Exit command uses X as its accelerator. It never has a short-
cut because it would be too easy to accidentally close the program while banging
your head on the keyboard. (Or if you fat-finger the keys, the keyboard is hit by a
flying tennis ball, your cat walks across the keyboard, and so on.)
3. Click the File item again. In the Type Here box to the right, type F&ormat.
4. Use the Type Here boxes below the Format menu to create the format menu items and
their submenus.
5. Use the Properties window to set the font sizes for the Font menu’s Small, Normal, and
Large items to 6, 9, and 20.
6. Give the Color and Font submenu items appropriate shortcuts.
7. Give the menu items that take action appropriate names. For example, name the Font
menu’s Small item
formatFontSmallMenuItem.
Add code behind the main menu items.
1. Double-click the Exit menu item and type the bold line in the following code so the
event handler looks like this:
private void leExitMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
The keyword this means “the object currently executing this code,” which in this
case means the current form, so this line of code tells the current form to close itself.
2. Double-click the Format➪  Color➪  Red menu item and type the bold line in the fol-
lowing code so the event handler looks like this:
private void formatColorRedMenuItem_Click(object sender, EventArgs e)
{
contentsTextBox.ForeColor = Color.Red;
}
3. Repeat step 2 for the Green and Blue menu items.
596906c05.indd 62 4/7/10 12:31:53 PM
Try It
63
4. Repeat step 2 for the Format➪  Background Color menu items, making them set the
TextBox’s BackColor property to Pink, LightGreen, and LightBlue.
5. Double-click the Format  ➪  Font➪  Small menu item and type the highlighted line in the
following code so the event handler looks like this:
private void formatFontSmallMenuItem_Click(object sender, EventArgs e)
{
contentsTextBox.Font = formatFontSmallMenuItem.Font;
}
6. Repeat step 5 for the Normal and Large menu items.
Make the context menu duplicate the main menu’s Format submenu.
Do either 1 or 2:
1. Build the structure from scratch. (This is straightforward but slow.)
a. Click the ContextMenuStrip in the Component Tray to open it for editing.
b. Use steps similar to the ones you used to build the main menu’s structure
to build the context menu’s structure. End context menu item names with
ContextMenuItem as in colorRedContextMenuItem.
2. Copy the Format menu’s structure. (This is sneakier and faster, and therefore
much cooler!)
a. Click the MenuStrip in the Component Tray to open it for editing. Expand the
Format menu. Click the Color item and then shift-click the Font item to select all
of the menu’s items. Press [Ctrl]+C to copy the menu items into the clipboard.
b. Click the ContextMenuStrip in the Component Tray to open it for editing. Press
[Ctrl]+V to paste the menu items into the context menu.
c. Give appropriate names to the new menu items.
Attach the context menu items to the event handlers used by the main menu.
1. Open the ContextMenuStrip for editing. Expand the Color submenu and click the Red
item. In the Properties window, click the events button (the lightning bolt) to see the
menu item’s events. Select the
Click event, click the dropdown arrow to the right, and
select
formatColorRedMenuItem_Click.
2. Repeat step 1 for the ContextMenuStrip’s other items, attaching them to the correct
event handlers.
Attach the context menu to the
TextBox.
1. Click the TextBox. In the Properties window, set its ContextMenuStrip property to
the
ContextMenuStrip formatContextMenu.
Please select Lesson 5 on the DVD to view the video that accompanies this lesson.
596906c05.indd 63 4/7/10 12:31:53 PM
64
LESSON 5 Making Menus
EXERCISES
1. (SimpleEdit) Copy the SimpleEdit program you started in Lesson 3, Exercise 1 (or download
Lesson 3’s version from the book’s web site at
www.wrox.com) and add the menu structure.
The following list shows the menu items. The items that display a dash (-) are separators.
Note the shortcuts and underlined accelerator keys. Add the code behind the Exit item, but
don’t worry about the other items yet.
File
New Ctrl+N
Open Ctrl+O
Save Ctrl+S
Save As...
-
Print Preview... Ctrl+P
Print...
-
Exit
Edit
Undo Ctrl+Z
Redo Ctrl+Y
-
Copy Ctrl+C
Cut Ctrl+X
Paste Ctrl+V
Delete Del
-
Select All
Format
Align
Left
Right
Center
Color...
Background Color...
Bullet
596906c05.indd 64 4/7/10 12:31:54 PM
Exercises
65
Offset
Normal
Subscript
Superscript
Font...
Indent
None
Hanging
Left
Right
Both
Eventually the user will be able to use
the Bullet menu item to toggle whether a
piece of text is bulleted. To allow C# to
toggle this item for you, set the menu item’s
CheckOnClick property to True.
Add a
ContextMenuStrip that duplicates the
Format menu and use it for the
TextBox’s
ContextMenuStrip property.
2. (SimpleEdit) Copy the SimpleEdit program you
built for Exercise 1 and add images to its menu
items. (You can find suitable images files in the
PngFiles directory of the Lesson 5 downloads
available on the book’s web site.) Figure 5-7
shows what the menus should look like when
you’re finished.
3. (SimpleEdit) Copy the SimpleEdit program
you built for Exercise 2 and add placeholder
routines for the menu items’ event handlers.
The routines should display simple message
boxes indicating what they should really do.
For example, the following code shows the File
menu’s
Save event handler.
private void leSaveMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(“Save”);
}
Add placeholders for all menu items (except separators) that do not have submenus. For
example, add a placeholder for the Format➪  Align  ➪  Left item but not for Format    Align
because it has a submenu.
FIGURE 57
596906c05.indd 65 4/7/10 12:31:54 PM
66
LESSON 5 Making Menus
Attach the context menu’s items to the same event handlers except give the context menu’s
Bullet item its own event handler. (If you make these two share the same event handler, they
will interfere with each other in Exercise 4 because of their toggling behavior.)
4. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 3 and add code to manage
exclusive selections in the Format menu’s Align, Offset, and Indent submenus. For example,
the user can select only one of the Align submenu’s choices at a time.
Modify the items’ placeholder code so when the user selects a choice, the code:
a. Checks the selected submenu item
b. Unchecks the other submenu items.
c. Checks the corresponding context menu item
d. Unchecks the corresponding context menu item
For example, the following code executes when the user selects the Align submenu’s
Left choice.
private void formatAlignLeftMenuItem_Click(object sender, EventArgs e)
{
formatAlignLeftMenuItem.Checked = true;
formatAlignRightMenuItem.Checked = false;
formatAlignCenterMenuItem.Checked = false;
alignLeftContextMenuItem.Checked = true;
alignRightContextMenuItem.Checked = false;
alignCenterContextMenuItem.Checked = false;
MessageBox.Show(“Align Left”);
}
5. (Simple Edit) Make the Format  ➪ Bullet menu item and the bullet context menu item check
and uncheck each other.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson05 folder in the download.
596906c05.indd 66 4/7/10 12:31:54 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.113.208