12.3 RichTextBoxThe RichTextBox is similar to the TextBox, but it has additional formatting capabilities. Whereas the TextBox control allows the Font property to be set for the entire control, the RichTextBox allows you to set the Font, as well as other formatting properties, for selections within the text displayed in the control.
When considering the difference between the TextBox and RichTextBox controls, compare Notepad and WordPad, the two text editors that are included with Windows. Notepad, based on the TextBox control, has no formatting applicable to selections within the text. WordPad, on the other hand, is practically a full-fledged word processor, with such features as bullets, hanging indents, fonts, and character styles
The default file format for the RichTextBox is the Rich Text Format (RTF), a file format that can carry formatting information.
The RichTextBox control inherits all the properties, methods, and events from the TextBoxBase class, so it behaves much like the TextBox control. In addition, it has many properties,
Table 12-7. Selected RichTextBox properties
Table 12-8. HorizontalAlignment enumeration values
Table 12-9. RichTextBoxSelectionTypes enumeration values
Table 12-10. RichTextBoxScrollBars enumeration values
The properties listed in Table 12-7 that begin with the word "Selection," such as SelectionAlignment or SelectionBullet, behave similarly: the property applies to the currently selected text. However, if there is currently no selected text, then it will
The listing in Example 12-5
This example creates a stripped-down version of WordPad, with a rich text box that fills the form client area and a truncated menu. (
When the program is compiled and run, you will get something like Figure 12-3. Figure 12-3. RichTextBox
Example 12-5. RichTextBox properties in C# (RichTextBoxes.cs)
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace ProgrammingWinApps
{
public class RichTextBoxes : Form
{
RichTextBox rtxt;
public RichTextBoxes( )
{
Text = "RichTextBox";
Size = new Size(400, 500);
rtxt = new RichTextBox( );
rtxt.Parent = this;
rtxt.Text = "Enter text here.";
rtxt.Rtf = "{\rtf1\pard Some RTF text.}";
rtxt.Multiline = true;
rtxt.BorderStyle = BorderStyle.Fixed3D;
rtxt.WordWrap = false;
rtxt.ScrollBars = RichTextBoxScrollBars.ForcedBoth;
rtxt.Dock = DockStyle.Fill;
rtxt.DetectUrls = true; // true is the default value
rtxt.AutoWordSelection = true;
rtxt.BulletIndent = 10;
rtxt.ShowSelectionMargin = true;
// Menus
// File menu items
MenuItem mnuImport = new MenuItem("&Import",
new EventHandler(mnuImport_Click));
MenuItem mnuFile = new MenuItem("&File",
new MenuItem[ ] {mnuImport});
// Edit menu items
MenuItem mnuDash1 = new MenuItem("-");
MenuItem mnuDash2 = new MenuItem("-");
MenuItem mnuUndo = new MenuItem("&Undo",
new EventHandler(mnuUndo_Click),
Shortcut.CtrlZ);
MenuItem mnuCut = new MenuItem("Cu&t",
new EventHandler(mnuCut_Click),
Shortcut.CtrlX);
MenuItem mnuCopy = new MenuItem("&Copy",
new EventHandler(mnuCopy_Click),
Shortcut.CtrlC);
MenuItem mnuCopyRtf = new MenuItem("Copy &Rtf",
new EventHandler(mnuCopyRtf_Click));
MenuItem mnuPaste = new MenuItem("&Paste",
new EventHandler(mnuPaste_Click),
Shortcut.CtrlV);
MenuItem mnuDelete = new MenuItem("&Delete",
new EventHandler(mnuDelete_Click));
MenuItem mnuSelectAll = new MenuItem("Select &All",
new EventHandler(mnuSelectAll_Click),
Shortcut.CtrlA);
MenuItem mnuSelect5 = new MenuItem("Select First &5",
new EventHandler(mnuSelect5_Click),
Shortcut.Ctrl5);
MenuItem mnuClear = new MenuItem("Clea&r",
new EventHandler(mnuClear_Click));
MenuItem mnuEdit = new MenuItem("&Edit",
new MenuItem[ ] {mnuUndo, mnuDash1,
mnuCut, mnuCopy, mnuCopyRtf, mnuPaste,
mnuDelete, mnuDash2, mnuSelectAll,
mnuSelect5, mnuClear});
// View Menu items
MenuItem mnuScrollToCaret = new MenuItem("&Scroll to Caret",
new EventHandler(mnuScrollToCaret_Click));
MenuItem mnuView = new MenuItem("&View",
new MenuItem[ ] {mnuScrollToCaret});
// Alignment menu items
MenuItem mnuAlignLeft = new MenuItem("Align&Left",
new EventHandler(mnuAlignLeft_Click));
MenuItem mnuAlignRight = new MenuItem("Align&Right",
new EventHandler(mnuAlignRight_Click));
MenuItem mnuAlignCenter = new MenuItem("Align&Center",
new EventHandler(mnuAlignCenter_Click));
// Format Menu items
MenuItem mnuBullet = new MenuItem("&Bullet",
new EventHandler(mnuBullet_Click));
MenuItem mnuAlign = new MenuItem("&Align",
new MenuItem[ ] {mnuAlignLeft,
mnuAlignRight, mnuAlignCenter});
MenuItem mnuRed = new MenuItem("&Red",
new EventHandler(mnuRed_Click));
MenuItem mnuBold = new MenuItem("Bo&ld",
new EventHandler(mnuBold_Click));
MenuItem mnuHang = new MenuItem("&Hanging Indent",
new EventHandler(mnuHang_Click));
MenuItem mnuIndent = new MenuItem("&Indent",
new EventHandler(mnuIndent_Click));
MenuItem mnuRightIndent = new MenuItem("&Right Indent",
new EventHandler(mnuRightIndent_Click));
MenuItem mnuFormat = new MenuItem("For&mat",
new MenuItem[ ] {mnuBullet, mnuAlign,
mnuRed, mnuBold, mnuHang, mnuIndent,
mnuRightIndent});
// Main menu
Menu = new MainMenu(new MenuItem[ ] {mnuFile, mnuEdit,
mnuView, mnuFormat});
} // close for constructor
static void Main( )
{
Application.Run(new RichTextBoxes( ));
}
private void mnuImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog( );
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)*.rtf" +
"All files (*.*)*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog( ) = = DialogResult.OK)
{
try
{
StreamReader reader = new StreamReader(ofd.FileName);
rtxt.Rtf = reader.ReadToEnd( );
reader.Close( );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuUndo_Click(object sender, EventArgs e)
{
if (rtxt.CanUndo = = true)
{
rtxt.Undo( );
rtxt.ClearUndo( );
}
}
private void mnuCut_Click(object sender, EventArgs e)
{
if (rtxt.SelectedText != "")
rtxt.Cut( );
}
private void mnuCopy_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.Copy( );
}
private void mnuCopyRtf_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
{
Clipboard.SetDataObject(rtxt.SelectedRtf);
}
}
private void mnuPaste_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject( ).GetDataPresent(DataFormats.Text)
= = true)
{
if (rtxt.CanUndo = = true)
{
if (rtxt.SelectionLength > 0)
{
if (MessageBox.Show(
"Do you want to overwrite the currently selected " +
"text?",
"Cut & Paste", MessageBoxButtons.YesNo) = =
DialogResult.No)
rtxt.SelectionStart = rtxt.SelectionStart +
rtxt.SelectionLength;
}
rtxt.Paste( );
}
}
}
private void mnuDelete_Click(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
rtxt.SelectedText = "";
}
private void mnuClear_Click(object sender, EventArgs e)
{
rtxt.Clear( );
}
private void mnuSelect5_Click(object sender, EventArgs e)
{
if (rtxt.Text.Length >= 5)
{
rtxt.Select(0,5);
}
else
{
rtxt.Select(0,rtxt.Text.Length);
}
}
private void mnuSelectAll_Click(object sender, EventArgs e)
{
rtxt.SelectAll( );
}
private void mnuScrollToCaret_Click(object sender, EventArgs e)
{
rtxt.ScrollToCaret( );
}
private void mnuBullet_Click(object sender, EventArgs e)
{
rtxt.SelectionBullet = !rtxt.SelectionBullet;
}
private void mnuAlignLeft_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Left;
}
private void mnuAlignRight_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Right;
}
private void mnuAlignCenter_Click(object sender, EventArgs e)
{
rtxt.SelectionAlignment = HorizontalAlignment.Center;
}
private void mnuRed_Click(object sender, EventArgs e)
{
if (rtxt.SelectionColor = = Color.Red)
rtxt.SelectionColor = Color.Black;
else
rtxt.SelectionColor = Color.Red;
}
private void mnuBold_Click(object sender, EventArgs e)
{
if (rtxt.SelectionFont.Bold )
rtxt.SelectionFont =
new Font(rtxt.SelectionFont, FontStyle.Regular);
else
rtxt.SelectionFont =
new Font(rtxt.SelectionFont, FontStyle.Bold);
}
private void mnuHang_Click(object sender, EventArgs e)
{
if (rtxt.SelectionHangingIndent = = 10 )
rtxt.SelectionHangingIndent = 0;
else
rtxt.SelectionHangingIndent = 10;
}
private void mnuIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionIndent = = 10 )
rtxt.SelectionIndent = 0;
else
rtxt.SelectionIndent = 10;
}
private void mnuRightIndent_Click(object sender, EventArgs e)
{
if (rtxt.SelectionRightIndent = = 50 )
rtxt.SelectionRightIndent = 0;
else
rtxt.SelectionRightIndent = 50;
}
} // close for form class
} // close form namespace
The program starts off by declaring a single class member variable, rtxt, representing the RichTextBox control. The equivalent VB.NET line of code would be:
dim rtxt as RichTextBox
In the constructor, both a Text property and an Rtf property are set. However, when you run the program, you will see that only the Rtf property is displayed in the control. If the order of these two lines of code are
Since RTF files use a backslash as a control character, and the backslash character is an escape character in C#, you must double the backslashes before they are properly escaped. Alternatively, you can use an @-quoted string literal in C#, as in:
rtxt.Rtf = @"{\rtf1\pard Some RTF text.}";
VB.NET does not use the backslash character as an escape character, so the equivalent line of code in VB.NET would be:
rtxt.Rtf = "{\rtf1\pard Some RTF text.}"
The ScrollBars property is set to ForcedBoth, which causes both a horizontal and vertical scrollbar to be present at all times, even if the contents of the control do not
The DockStyle property is set to Fill, which forces the rich text box to fill the client area of the form. The DockStyle property was explained in Chapter 7. The DetectUrls property is set to true , although the line is not strictly necessary, since this is the default value. However, by changing it to false , you can see the different behavior when you enter any valid URL in the control. The AutoWordSelection property is set to true . This property causes the entire word to be selected if any part of the word is selected.
The BulletIndent and ShowSelectionMargin properties work as described in Table 12-7. The menus are similar to those used in Example 12-1 and Example 12-2, with some additional menu items. The main menu now has File, Edit, View, and Format items. Under File, an Import command lets you import an RTF file. The Edit menu item has an additional command, CopyRtf, which allows you to copy the selected text as RTF rather than plain text. The View menu has only the Scroll To Caret command, while the Format menu is entirely new, demonstrating several of the rich text box formatting capabilities. The menu item declarations use a different syntax in VB.NET from that in C# for assigning event handlers. A typical menu item declaration looks like:
MenuItem mnuUndo = new MenuItem("&Undo",
new EventHandler(mnuUndo_Click),
Shortcut.CtrlZ);
dim mnuUndo as new MenuItem("&Undo", _
new EventHandler(AddressOf mnuUndo_Click), _
Shortcut.CtrlZ)
These menu item declarations were described
The File
private sub mnuImport_Click(ByVal sender As Object, _
ByVal e As EventArgs)
dim ofd as new OpenFileDialog( )
ofd.InitialDirectory = "c:\"
ofd.Filter = "RTF files (*.rtf)*.rtf" + _
"All files (*.*)*.*"
ofd.FilterIndex = 1 ' 1 based index
if ofd.ShowDialog( ) = DialogResult.OK then
try
dim reader as new StreamReader(ofd.FileName)
rtxt.Rtf = reader.ReadToEnd( )
reader.Close( )
catch ex as Exception
MessageBox.Show(ex.Message)
return
end try
end if
end sub
In either language, a new OpenFileDialog is
using System.IO;
imports System.IO
The next method of note is mnuCopyRtf_Click, which implements the Edit
Figure 12-4. RichTextBox with selected text
If the Edit
Figure 12-5. WordPad with pasted RTF selections
The Format menu looks like that shown in Figure 12-6. This is hardly a
Figure 12-6. RichTextBoxes — Format menu
Most of these menu items are simple toggles (e.g., if the selection is not bulleted, then bullet it;
rtxt.SelectionBullet = !rtxt.SelectionBullet;
rtxt.SelectionBullet = not rtxt.SelectionBullet
The Format
if (rtxt.SelectionColor = = Color.Red)
rtxt.SelectionColor = Color.Black;
else
rtxt.SelectionColor = Color.Red;
if rtxt.SelectionColor.Equals(Color.Red) then rtxt.SelectionColor = Color.Black else rtxt.SelectionColor = Color.Red end if
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
The Bold, Hanging Indent, Indent, and Right Indent formatting commands are similar to the Red formatting command (which tests to see if the desired property is current and sets the property
The final formatting commands are the three Align commands: AlignLeft, AlignCenter, and AlignRight. Although these commands are not toggles, apply the appropriate property:
rtxt.SelectionAlignment = HorizontalAlignment.Center; 12.3.1 RichTextBox Methods and EventsAs you would expect, the RichTextBox control has several methods and events in addition to those it inherits. Some of the most commonly used methods are listed in Table 12-11, with commonly used events listed in Table 12-16. (Tables Table 12-12 through Table 12-15 contain detailed information about the methods.) The listing in Example 12-6 demonstrates several of the RichTextBox methods and events using C#. As with the previous example, this program is lengthy and virtually identical to the VB.NET version (aside from the normal syntactic differences). Only the C# version is presented in its entirety. Sections of code that are significantly different in VB.NET will be shown. The complete VB.NET source code can be downloaded from http://www.LibertyAssociates.com (click on Books). Table 12-11. RichTextBox methods
Table 12-12. RichTextBoxFind method overload argument list
Table 12-13. RichTextBoxFinds enumeration values
Table 12-14. RichTextBox LoadFile method overload argument list
Table 12-15. RichTextBoxStreamType enumeration values
Table 12-16. RichTextBox events
Example 12-6. RichTextBox methods and events in C# (RichTextBoxMethodsEvents.cs)
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
namespace ProgrammingWinApps
{
public class RichTextBoxMethodsEvents : Form
{
RichTextBox rtxt;
string strFileName;
public RichTextBoxMethodsEvents( )
{
Text = "RichTextBox Methods & Events";
Size = new Size(400, 500);
rtxt = new RichTextBox( );
rtxt.Parent = this;
rtxt.Rtf = "{\rtf1\pard Some RTF text.}";
rtxt.Multiline = true;
rtxt.BorderStyle = BorderStyle.Fixed3D;
rtxt.ScrollBars = RichTextBoxScrollBars.ForcedVertical;
rtxt.Dock = DockStyle.Fill;
rtxt.AutoWordSelection = true;
rtxt.BulletIndent = 10;
rtxt.ShowSelectionMargin = true;
rtxt.LinkClicked += new LinkClickedEventHandler(Link_Click);
rtxt.SelectionChanged += new EventHandler(Selection_Changed);
// Menus
// File menu items
MenuItem mnuImport = new MenuItem("&Import...",
new EventHandler(mnuImport_Click),
Shortcut.CtrlI);
MenuItem mnuLoad = new MenuItem("&Load...",
new EventHandler(mnuLoad_Click),
Shortcut.CtrlL);
MenuItem mnuSaveAs = new MenuItem("Save &As...",
new EventHandler(mnuSaveAs_Click));
MenuItem mnuFile = new MenuItem("&File",
new MenuItem[ ] {mnuImport, mnuLoad, mnuSaveAs});
// Edit menu items
MenuItem mnuFind = new MenuItem("&Find",
new EventHandler(mnuFind_Click),
Shortcut.CtrlF);
MenuItem mnuRedo = new MenuItem("&Redo",
new EventHandler(mnuRedo_Click));
MenuItem mnuEdit = new MenuItem("&Edit",
new MenuItem[ ] {mnuFind, mnuRedo});
// Main menu
Menu = new MainMenu(new MenuItem[ ] {mnuFile, mnuEdit});
} // close for constructor
static void Main( )
{
Application.Run(new RichTextBoxMethodsEvents( ));
}
private void mnuImport_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog( );
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)*.rtf" +
"All files (*.*)*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog( ) = = DialogResult.OK)
{
try
{
StreamReader reader = new StreamReader(ofd.FileName);
rtxt.Rtf = reader.ReadToEnd( );
reader.Close( );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuLoad_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog( );
ofd.InitialDirectory = @"c:\";
ofd.Filter = "RTF files (*.rtf)*.rtf" +
"Text files (*.txt)*.txt" +
"All files (*.*)*.*";
ofd.FilterIndex = 1; // 1 based index
if (ofd.ShowDialog( ) = = DialogResult.OK)
{
try
{
// Determine the file extension
if (ofd.FileName.EndsWith(".rtf"))
rtxt.LoadFile(ofd.FileName);
else
{
if (ofd.FileName.EndsWith(".txt"))
rtxt.LoadFile(ofd.FileName,
RichTextBoxStreamType.PlainText);
else
MessageBox.Show("Invalid file format.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuSaveAs_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog( );
sfd.InitialDirectory = @"c:\";
sfd.Filter = "RTF files (*.rtf)*.rtf" +
"Text files (*.txt)*.txt" +
"All files (*.*)*.*";
sfd.FilterIndex = 1; // 1 based index
if (strFileName != null)
sfd.FileName = strFileName;
else
sfd.FileName = "*.rtf";
if (sfd.ShowDialog( ) = = DialogResult.OK)
{
try
{
strFileName = sfd.FileName;
// Determine the file extension
if (strFileName.EndsWith(".rtf"))
rtxt.SaveFile(strFileName);
else
{
if (strFileName.EndsWith(".txt"))
rtxt.SaveFile(strFileName,
RichTextBoxStreamType.PlainText);
else
MessageBox.Show("Invalid file format.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
private void mnuFind_Click(object sender, EventArgs e)
{
// use the current selection as the string to find.
if (rtxt.SelectionLength > 0)
{
string str = rtxt.SelectedText;
int intStart = rtxt.Find(str,
rtxt.SelectionStart + rtxt.SelectionLength,
-1, RichTextBoxFinds.None);
if (intStart < 0)
MessageBox.Show("String not found.");
}
else
MessageBox.Show("Nothing to find.");
}
private void mnuRedo_Click(object sender, EventArgs e)
{
if (rtxt.CanRedo)
rtxt.Redo( );
}
private void Link_Click(object sender, LinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(e.LinkText);
}
private void Selection_Changed(object sender, EventArgs e)
{
if (rtxt.SelectionLength > 0)
MessageBox.Show("You have changed the selection to \"" +
rtxt.SelectedText + "\"");
}
} // close for form class
} // close form namespace
When the program listed in Example 12-6 is compiled and run, it looks similar to that shown for the previous example in Figure 12-3, except that the menu is sparser and there is no horizontal scrollbar. The code in Example 12-6 is similar to that listed in Example 12-5. The constructor has two lines that add two event handlers for LinkClicked and SelectionChanged events, respectively:
rtxt.LinkClicked += new LinkClickedEventHandler(Link_Click); rtxt.SelectionChanged += new EventHandler(Selection_Changed);
AddHandler rtxt.LinkClicked, AddressOf Link_Click AddHandler rtxt.SelectionChanged, AddressOf Selection_Changed
The menu is significantly
As before, most action occurs in the event handler methods. The event handler for importing an RTF file, mnuImport_Click, is unchanged from the previous example. It is left in here simply as a comparison with the Load menu item. The handler for that item, mnuLoad_Click, uses the same OpenFileDialog common dialog, but rather than use a StreamReader to stream the file into the Rtf property of the control, it uses the LoadFile instance method. Another difference between the Import and Load event handlers is that the Load event handler allows both RTF and text files, reflected in the OpenFileDialog Filter property:
ofd.Filter = "RTF files (*.rtf)*.rtf" +
"Text files (*.txt)*.txt" +
"All files (*.*)*.*";
Then within the try block where the selected file is actually loaded, the string EndsWith instance method determines if the selected file is an RTF or a TXT file. If it is the former, the LoadFile method is invoked with only the filename as an argument. If it is the latter, then the LoadFile method is invoked with RichTextBoxStreamType.PlainText passed as an argument:
try
{
// Determine the file extension
if (ofd.FileName.EndsWith(".rtf"))
rtxt.LoadFile(ofd.FileName);
else
{
if (ofd.FileName.EndsWith(".txt"))
rtxt.LoadFile(ofd.FileName,
RichTextBoxStreamType.PlainText);
else
MessageBox.Show("Invalid file format.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
try
' Determine the file extension
if ofd.FileName.EndsWith(".rtf") then
rtxt.LoadFile(ofd.FileName)
else
if (ofd.FileName.EndsWith(".txt"))
rtxt.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText)
else
MessageBox.Show("Invalid file format.")
end if
end if
catch ex as Exception
MessageBox.Show(ex.Message)
return
end try
The File
Most real-world applications implement a Find function by presenting a dialog box for the user to enter the search string. The Edit
Assuming there is something selected, the selected text is assigned to a string variable, which is then passed to the Find method, along with several other arguments. Seven overloaded forms of the Find method are listed in Table 12-12, providing the ability to search for either a character or a string, either in the entire control or in a specified range. If it is a string, it uses a combination of RichTextBoxFinds enumeration values, listed in Table 12-13, to refine the search. In any case, if the search is successful, the Find method returns the zero-based index of the first character of the string that is found. It also highlights the found string. If nothing is found, then the method returns -1. In this example, four arguments are passed to the Find method: the search string, the starting index, the ending index, and a RichTextBoxFinds enumeration value:
int intStart = rtxt.Find(str,
rtxt.SelectionStart + rtxt.SelectionLength,
-1, RichTextBoxFinds.None);
dim intStart as integer = rtxt.Find(str, _
rtxt.SelectionStart + rtxt.SelectionLength, _
-1, RichTextBoxFinds.None)
Notice that the starting index is set to the index at the start of the selection plus the length of the selection, which effectively starts the search at the end of the selection. If you started the search at the start of the selection, it would just find itself. The ending index is set to -1, which has the effect of searching to the end of the control contents. The following line would be equivalent to the line used:
int intStart = rtxt.Find(str,
rtxt.SelectionStart + rtxt.SelectionLength,
RichTextBoxFinds.None);
Since none of the overloaded forms of the method allows searching for a string from a specified starting point without passing a RichTextBoxFinds parameter, even if there is no refinement to the desired search, the None enumerated value is passed in.
If the search is successful, the found text is highlighted and the character index of the first character of the found string is assigned to the integer intStart. If nothing is found, then -1 would be returned. intStart is
The Edit
Two event handlers in Example 12-6 are not associated with any menu item. The Link_Click event handler is invoked whenever a URL in the control is clicked. This event handler was assigned to the LinkClicked event back in the constructor with the following line:
rtxt.LinkClicked += new LinkClickedEventHandler(Link_Click);
AddHandler rtxt.LinkClicked, AddressOf Link_Click
This event
The SelectionChanged event is raised every time the selection is changed in the text box. It is assigned to the Selection_Changed event handler method with the appropriate line of code.
rtxt.SelectionChanged += new EventHandler(Selection_Changed);
AddHandler rtxt.SelectionChanged, AddressOf Selection_Changed The Selection_Changed event handler first tests to see if anything is selected (i.e., the selection length is greater than zero) to prevent the message box from popping up every time you deselect. Anything that is selected is displayed in a message box. |