Using RichTextBoxes
The TextBox control lets the user enter text and that’s about it. It can display its text in different
colors and fonts, but it cannot give different pieces of text different properties. The
TextBox is
intended to let the user enter a simple string, like a name or street address, and very little more.
The
RichTextBox is a much more powerful control. It can display different pieces of text with
different colors, fonts, and styles. It can adjust paragraph indentation and make bulleted lists.
It can even include pictures. Its not as powerful as a full-featured word processor, such as
Microsoft Word or OpenOffice’s Writer, but it can produce a much more sophisticated result
than the
TextBox.
In this lesson you learn more about the
RichTextBox control and how to use it. You have
a chance to experiment with the control, and you use it to add enough functionality to the
SimpleEdit program to finally make the program useful.
USING RICHTEXTBOX PROPERTIES
To change the appearance of the text inside a RichTextBox, you first select the text that you
want to change, and then you set one of the control’s properties to modify the text.
To select the text, you use the control’s
SelectionStart and SelectionLength properties
to indicate where the text begins and how many letters it includes. Note that the letters are
numbered starting with 0. (In fact, almost all numbering starts with 0 in C#.) For example,
setting
SelectionStart = 0 and SelectionLength = 1 selects the control’s first letter.
After you select the text, you set one of the
RichTextBoxs properties to the value that you
want the text to have.
For example, the following code makes the
RichTextBox named rchContent display some
text and color the word “red.
contentRichTextBox.Text = “Some red text”;
contentRichTextBox.SelectionStart = 5;
contentRichTextBox.SelectionLength = 3;
contentRichTextBox.SelectionColor = Color.Red;
7
596906c07.indd 79 4/7/10 12:32:04 PM
80
LESSON 7 Using RichTexTBoxes
Table 7-1 lists properties that you can use to change the text’s appearance.
TABLE 71
PROPERTY PURPOSE
SelectionAlignment
Aligns the selection’s paragraph on the left, center, or right.
SelectionBackColor
Sets the selection’s background color.
SelectionBullet
Determines whether the selection’s paragraph is bulleted.
SelectionCharOffset
Determines whether the selection is superscript (oset > 0), sub-
script (oset < 0), or normal (oset = 0).
SelectionColor
Sets the selection’s color.
SelectionFont
Sets the selection’s font.
SelectionHangingIndent
The first line in the selection’s paragraph is indented normally
and then subsequent lines in the paragraph are indented by
this amount.
SelectionIndent
All lines are indented by this amount.
SelectionProtected
Marks the selected text as protected so the user cannot modify it.
SelectionRightIndent
All lines are indented on the right by this amount.
The FontFeatures example program shown in Figure 7-1 demonstrates properties that
change the appearance of text within a paragraph. These include the
SelectionBackColor,
SelectionCharOffset, SelectionColor, and SelectionFont.
The ParagraphFeatures program shown in Figure 7-2 demonstrates properties that change the
way paragraphs are displayed. These include
SelectionIndent, SelectionHangingIndent,
SelectionRightIndent, SelectionBullet, and SelectionAlignment.
FIGURE 71 FIGURE 72
596906c07.indd 80 4/7/10 12:32:04 PM
Giving the User Control
81
Both the FontFeatures and ParagraphFeatures sample programs are available as
part of the Lesson 7 download at
www.wrox.com.
Table 7-2 summarizes four additional properties that change the text displayed by the control that
deserve special mention.
TABLE 72
PROPERTY PURPOSE
Text
Gets or sets the control’s text without any formatting properties.
Rtf
Gets or sets the control’s Rich Text Format (RTF) contents. This includes
the text plus RTF formatting codes that define how the text should be
displayed.
SelectedText
Gets or sets the selection’s text.
SelectedRtf
Gets or sets the selection’s text and RTF codes.
GIVING THE USER CONTROL
Allowing the user to change text settings is easy. When
the user selects text in the control, the
RichTextBox sets
its
SelectionStart and SelectionLength properties
accordingly. All you need to do is set the appropriate
property (for example,
SelectionColor) and the selected
text is updated.
The SetTextProperties example program shown in
Figure 7-3 (and available as part of the Lesson 7 code
download) uses this technique to let the user control text
color, character offset, and paragraph alignment. Select
text and then click the tool strip buttons to change the
text’s properties.
The following code shows how the SetTextProperties program changes the currently selected text to
have a black background and white foreground.
private void reverseColorsButton_Click(object sender, EventArgs e)
{
contentRichTextBox.SelectionBackColor = Color.Black;
contentRichTextBox.SelectionColor = Color.White;
}
The programs other buttons work similarly.
FIGURE 73
596906c07.indd 81 4/7/10 12:32:05 PM
82
LESSON 7 Using RichTexTBoxes
USING RICHTEXTBOX METHODS
Lesson 2 briefly described properties, methods, and events. Other lessons have also worked with
many properties and events. In fact, most of the event handlers I’ve discussed in the lessons so far
catch an event and change a property in response.
Though you’ve worked with many properties and events, the only method you’ve seen is the forms
Close method, which makes the form go away. For example, the following code closes the form that
executes it:
this.Close();
The RichTextBox provides many new methods that are quite helpful for building a text editing
program. Table 7-3 summarizes some of the most useful.
TABLE 73
METHOD PURPOSE
Clear
Clears all text from the control.
Copy
Copies the current selection into the clipboard.
Cut
Cuts the current selection into the clipboard.
DeselectAll
Deselects all text by setting SelectionLength = 0.
LoadFile
Loads the control’s text from a file with one of various formats such as RTF or
plain text.
Paste
Pastes whatever is in the clipboard into the current selection. This can be
anything that the
RichTextBox understands such as text, RTF formatted
text, or an image.
Redo
Redoes the previously undone command.
SaveFile
Saves the control’s text into a file in one of various formats such as RTF or
plain text.
SelectAll
Selects all of the control’s text by setting SelectionStart = 0 and
SelectionLength equal to the text’s length.
Undo
Undoes the most recent change.
The following code shows how a program can use the LoadFile method. The first parameter gives the
name of the file, which can be relative to the program’s current directory or a full path. The second
parameter gives the type of file.
rchContent.LoadFile(“Test.rtf”, RichTextBoxStreamType.RichText);
596906c07.indd 82 4/7/10 12:32:05 PM
Using RichTextBox Methods
83
TYPING TIPS
When you type contentRichTextBox.LoadFile(, IntelliSense displays a popup
showing the parameters that the
LoadFile method expects, as shown in Figure 7-4.
(Visual Studio puts the red squiggly underline below the code in Figure 7-4 because
the statement isn’t finished yet. Until I finish typing the statement, Visual Studio
flags it as an error.)
There are several different overloaded versions of the method that take different
parameters to choose from. Overloaded versions of a method have the same name
but take different parameters.
Use the up and down arrow keys to scroll through the method’s available versions.
FIGURE 74
As you enter parameters, IntelliSense updates to describe the next parameter that
it expects. Figure 7-5 shows the
LoadFile method after I entered a filename for the
rst parameter. IntelliSense shows that the next parameter should be a value of type
RichTextBoxStreamType named fileType. IntelliSense even shows a short descrip-
tion of what the value means at the bottom (although it’s not super informative).
FIGURE 75
You could type in RichTextBoxStreamType followed by a dot to see a list of
available choices, but there’s an even easier (that is, better) way to do this: press
[Ctrl]+[Space]. That makes IntelliSense display a list of things that you might be
trying to type. At this point, IntelliSense is smart enough to guess that you want
to type
RichTextBoxStreamType so it initially selects that type and even displays
more information about it, as shown in Figure 7-6.
FIGURE 76
continues
596906c07.indd 83 4/7/10 12:32:05 PM
..................Content has been hidden....................

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