72
LESSON 6 Making Tool STripS and STaTuS STripS
Initially check the Text Color menu’s Black choice and the Background Color menu’s
White choice.
Give the Background Color menu items
Images that display samples of the color.
Create the
ToolStrip with buttons that duplicate the menu hierarchy. The ToolStrip
should hold two
ToolStripDropDownButtons.
Name the first tool
ToolStripDropDownButton textColorButton and make it display the
text “A.” Give it the items Black, Red, Green, and Blue. Each item should have
ForeColor
property set to its color.
Name the second tool
ToolStripDropDownButton backColorButton and make it initially
display a white color sample. Give it the items White, Pink, Green, and Blue. Make each of
these display an
Image showing a sample of the color.
Give the
StatusStrip a ToolStripStatusLabel named colorLabel with Text =
Text Colors.
Add event handlers.
Make the File menus Exit item close the form.
Make event handlers for each of the Text Color menu items.
Set the
RichTextBox control’s ForeColor property to the selected color.
Set the
ForeColor property of the textColorMenuItem submenu to the appropri-
ate color.
Set the
ForeColor property of the textColorButton tool strip button to the appro-
priate color.
Set the
ForeColor property of the colorLabel label to the appropriate color.
Check the selected menu item and deselect the other Text Color menu items.
Check the corresponding tool strip button and deselect the other Text Color tool
strip buttons.
Make event handlers for each of the Background Color menu items.
Set the
RichTextBox control’s BackColor property to the selected color.
Set the
Image property of the backColorMenuItem submenu to display a sample of
the appropriate color.
Set the
Image property of the backColorButton tool strip button to display a sample
of the appropriate color.
Set the
BackColor property of the colorLabel label to the appropriate color.
Check the corresponding menu item and deselect the other Background Color
menu items.
Check the selected tool strip button and deselect the other Background Color tool
strip buttons.
Use the menu items’ event handlers for the corresponding tool strip button event handlers.
596906c06.indd 72 4/7/10 12:32:00 PM
Try It
73
DUPLICATE CODE
As you will probably notice, this lessons Try It includes event handlers that dupli-
cate the same code with minor differences. In general, if large pieces of code do
almost the same things with minor changes, then there’s probably something wrong
with the program’s design.
In cases such as this, you should extract the common code into a function. You can
use
if, switch, and other C# statements to let the code take different actions for
different situations, allowing the same function to handle multiple situations.
Unfortunately, you don’t know how to do any of that yet, but you will learn.
Lesson 20 explains how to write functions, and Lesson 18 describes statements
such as
if and switch. Until then, youre stuck with some duplicate code.
After you read Lessons 18 and 20, you can revisit this code if you like to remove
the redundant code, making it easier to maintain in the future. (The process of
restructuring existing code to make it more reliable, easier to read, easier to main-
tain, or otherwise better without changing its functionality is called refactoring.)
Hints
Recall that the E
xit menu item can close the program’s form by calling this.Close().
Place the
RichTextBox inside the ToolStripContainer’s content panel.
Step-by-Step
Create the form shown in Figure 6-6.
1. Start a new project.
2. Add a MenuStrip to the form.
3. Add a StatusStrip to the form.
4. Add a ToolStripContainer to the form.
5. Add a RichTextBox named contentRichTextBox inside the ToolStripContainer’s
content panel.
Create the
MenuStrip.
1. Add the indicated menu items to the MenuStrip. Remember to give them good names
and appropriate accelerator keys.
Initially check the Text Color menu’s Black choice and the Background Color menu’s
White choice.
1. Set the Text ColorBlack menu item’s Checked property to True.
596906c06.indd 73 4/7/10 12:32:00 PM
74
LESSON 6 Making Tool STripS and STaTuS STripS
2. Set the Background ColorWhite menu item’s Checked property to True.
Give the Background Color menu items
Images that display samples of the color.
1. Set the Image properties of these menu items to samples of their colors. (Use Microsoft
Paint or some other graphical editor to make small colored images.)
Create the
ToolStrip with buttons that duplicate the menu hierarchy. The ToolStrip
should hold two
ToolStripDropDownButtons.
Name the first tool
ToolStripDropDownButton textColorButton and make it display the
text “A.” Give it the items Black, Red, Green, and Blue. Each item should have
ForeColor
property set to its color.
1. Create the ToolStripDropDownButton.
2. Below that item, add the items Black, Red, Green, and Blue.
3. Set the ForeColor property for each of these items to show its color (that is, set the
Black item’s
ForeColor property to black).
Name the second tool
ToolStripDropDownButton backColorButton and make it initially
display a white color sample. Give it the items White, Pink, Green, and Blue. Make each of
these display an
Image showing a sample of the color.
1. Create the ToolStripDropDownButton.
2. Below that item, add the items White, Pink, Green, and Blue.
3. Set the Image property for each of these items to show samples of their colors.
Give the
StatusStrip a ToolStripStatusLabel named colorLabel with Text =
Text Colors.
1. Create the ToolStripStatusLabel. Set its Name and Text properties.
Add event handlers.
Make the File menus Exit item close the form.
1. Type the bold line of code so the event handler looks like this:
private void leExitMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
Make event handlers for each of the Text Color menu items.
1. For the Text ColorBlack menu item, type the bold code so the event handler looks
like this:
private void textColorBlackMenuItem_Click(object sender, EventArgs e)
{
contentsRichTextBox.ForeColor = Color.Black;
textColorMenuItem.ForeColor = Color.Black;
textColorButton.ForeColor = Color.Black;
colorLabel.ForeColor = Color.Black;
596906c06.indd 74 4/7/10 12:32:01 PM
Try It
75
textColorBlackMenuItem.Checked = true;
textColorRedMenuItem.Checked = false;
textColorGreenMenuItem.Checked = false;
textColorBlueMenuItem.Checked = false;
textColorBlackToostripMenuItem.Checked = true;
textColorRedToostripMenuItem.Checked = false;
textColorGreenToostripMenuItem.Checked = false;
textColorBlueToostripMenuItem.Checked = false;
}
2. Enter similar code for the other Text Color menu items.
Make event handlers for each of the Background Color menu items.
1. For the Background ColorWhite menu item, type the bold code so the event handler
looks like this:
private void backColorWhiteMenuItem_Click(object sender, EventArgs e)
{
contentsRichTextBox.BackColor = Color.White;
backColorMenuItem.Image = backColorWhiteMenuItem.Image;
backColorButton.Image = backColorWhiteMenuItem.Image;
colorLabel.BackColor = Color.White;
backColorWhiteMenuItem.Checked = true;
backColorPinkMenuItem.Checked = false;
backColorGreenMenuItem.Checked = false;
backColorBlueMenuItem.Checked = false;
backColorWhiteToostripMenuItem.Checked = true;
backColorPinkToostripMenuItem.Checked = false;
backColorGreenToostripMenuItem.Checked = false;
backColorBlueToostripMenuItem.Checked = false;
}
2. Enter similar code for the other Background Color menu items.
Use the menu items’ event handlers for the corresponding tool strip button event handlers.
1. Click the Properties window’s Events button.
2. For each tool strip button:
a. Click the button in the Form Editor.
b. On the Properties window, select the Click event. Then click the dropdown
arrow to the right.
c. Select the appropriate menu event handler. For example, for the
textColorBlackToolstripMenuItem tool strip button, select
the
textColorBlackMenuItem_Click event handler.
Please select Lesson 6 on the DVD to view the video that accompanies this lesson.
596906c06.indd 75 4/7/10 12:32:01 PM
76
LESSON 6 Making Tool STripS and STaTuS STripS
EXERCISES
1. (SimpleEdit) Copy the SimpleEdit program you built in Lesson 5, Exercise 5 (or download
Lesson 5’s version from the book’s web site) and add the tool strips, buttons, and separators
shown in Figure 6-7. Delete the
RichTextBox control, add a ToolStripContainer, and then
re-add the
RichTextBox inside the ToolStripContainer’s content panel.
FIGURE 67
The black button (fourth from the left on the second tool strip row) is a
ToolStripSplitButton that lets the user pick a text color. It contains the choices
Black, White, Red, Green, and Blue.
The white button next to the text color button is another
ToolStripSplitButton that lets
the user pick a background color. It contains the choices Black, White, Pink, Green, Blue,
and Yellow.
The button that says “AB” is a
ToolStripDropDownButton that provides the same options
as the Format menu’s Offset submenu: Normal, Superscript, and Subscript.
2. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 1 and add menu item code
to manage the new tool strip buttons. Add code to synchronize corresponding menu, context
menu, and tool strip button items. For example, the following shows the new code for the
Align Left menu item:
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;
alignLeftButton.Checked = true;
alignRightButton.Checked = false;
alignCenterButton.Checked = false;
MessageBox.Show(“Align Left”);
}
596906c06.indd 76 4/7/10 12:32:01 PM
..................Content has been hidden....................

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