84
LESSON 7 Using RichTexTBoxes
(continued)
Now you can press [Tab] to make IntelliSense fill in the highlighted value
RichTextBoxStreamType for you.
Finally, press the “.” key to see the list of choices shown in Figure 7-7, pick one, and
press [Tab] to add it to the code. Finally add “);” and you’re done.
FIGURE 77
I know this sounds like a big mess, but with a little practice it becomes surprisingly
quick and easy. Typing everything by hand, I can enter the previous
LoadFile state-
ment in about 30 seconds. With IntelliSense’s help, I can type the same line in under
10 seconds.
The following code shows how a program can use the
SaveFile method. As with LoadFile, the
rst parameter gives the file’s name and the second gives its type.
contentRichTextBox.SaveFile(“Test.rtf”, RichTextBoxStreamType.RichText);
TRY IT
In this Try It, you add functionality to some of
the SimpleEdit program’s menu items and tool
strip buttons. You use the
RichTextBox proper-
ties and methods to implement the commands in
the Edit menu: Undo, Redo, Copy, Cut, Paste,
Delete, and Select All. (This also makes the cor-
responding buttons work at no extra charge.)
Figure 7-8 shows the program with its Edit
menu open.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson07 folder in the download.
FIGURE 78
596906c07.indd 84 4/7/10 12:32:06 PM
Try It
85
Lesson Requirements
In this lesson, you:
Copy the SimpleEdit program you built in Lesson 6, Exercise 5.
Add code to handle the Edit menu’s commands.
Add Undo code.
Add Redo code.
Add Copy code.
Add Cut code.
Add Paste code.
Add Delete code.
Add Select All code.
Hints
For the Delete menu item, simply set the control’s
SelectedText property to an
empty string
“”.
Step-by-Step
Copy the SimpleEdit program you built in Lesson 6, Exercise 5. If you skipped that exercise,
download the Lesson 6 material from the book’s web site at www.wrox.com and use the
version it contains.
Add code to handle the Edit menu’s commands.
1. Open the program’s form in the Form Designer. Click the MenuStrip, expand the Edit
menu, and double-click the Undo menu item.
2. Replace the placeholder call to MessageBox.Show with the following line of code so the
event handler looks like this:
private void editUndoMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Undo();
}
3. Repeat the previous two steps for the other Edit menu items. The following code shows
the new event handlers:
private void editUndoMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Undo();
}
596906c07.indd 85 4/7/10 12:32:06 PM
86
LESSON 7 Using RichTexTBoxes
private void editRedoMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Redo();
}
private void editCopyMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Copy();
}
private void editCutMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Cut();
}
private void editPasteMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.Paste();
}
private void editDeleteMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.SelectedText = “”;
}
private void editSelectAllMenuItem_Click(object sender, EventArgs e)
{
contentRichTextBox.SelectAll();
}
When you finish, test the program’s new features. One of the RichTextBox’s more remarkable fea-
tures is its ability to paste different kinds of items from the clipboard. For example, copy a picture to
the clipboard and then use the program to paste it into the RichTextBox.
Please select Lesson 7 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. (SimpleEdit) Copy the SimpleEdit program you built for the Try It and add simple code to
handle the File menu’s New, Open, Save, and Exit commands. For the New command, simply
clear the
RichTextBox. For the Open and Save commands, just load and save the file “Test.
rtf.” (The program will create the file the first time you save. If you try to open the file before it
exists, the program will crash so don’t use Open before you use Save.) Lesson 8 explains how
to use file open and save dialogs to let the user pick the file that should be opened or saved.
2. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 1 and add code to handle
the Format menu’s commands (except for the Font command, which is covered in Lesson 8).
Remove the placeholder
MessageBox.Show commands. Figure 7-9 shows the program with
its Format menu and Align submenu expanded.
596906c07.indd 86 4/7/10 12:32:07 PM
Exercises
87
FIGURE 79
Hints:
Keep the code that manages the menu items and tool strip buttons. For example,
keep the code that makes sure only one alignment menu item and button is selected
at a time.
To set the foreground color, use the color stored in
foreColorButton.ForeColor.
To set the background color, use the color stored in
backColorButton.BackColor.
Add event handlers to the foreground and background color tool strip menu items.
For example, give an event handler to the button that gives the selected text a red
foreground color.
Add event handlers to the
foreColorButton and backColorButton buttons so
when they are clicked, they apply the current color settings. This lets the user reapply
the color setting previously selected by one of the sub-buttons.
Make the indentation commands (None, Hanging, Left, Right, and Both) reset any other
indentations. For example, the Hanging command should set the SelectionIndent and
SelectionRightIndent properties to 0 as in the following code:
contentRichTextBox.SelectionIndent = 0;
contentRichTextBox.SelectionRightIndent = 0;
contentRichTextBox.SelectionHangingIndent = 20;
3. The SimpleEdit program allows only the indentation styles None, Hanging, Left, Right, and
Both. It doesn’t allow other combinations such as Hanging plus Left. Build a program that
uses tool strip buttons to let the user select each of the indentation properties (hanging, left,
and right) individually. Provide a fourth button to clear all of the indentation properties.
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 Lesson07 folder in the download.
596906c07.indd 87 4/7/10 12:32:07 PM
Click here to Play
596906c07.indd 88 4/7/10 12:32:07 PM
..................Content has been hidden....................

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