|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article describes an easy approach to building a simple word processor around an extended version of the Rich Text Box (RTB) control; Microsoft has made available an extended version of the RTB control that greatly eases the requirements for printing the control's text or RTF content. This article and the sample application will use this extended version of the RTF control to demonstrate the following word processor related functions:
Whilst this project will not lead you to drop MS Word, it is a decent little word processor and to that end, you may find a use for it. I have always kept one of these on hand and have frequently tailored them to do things like provide a custom tool for viewing reports, error logs, and things of that nature when I wanted to integrate that feature into a product so that I could use it in lieu of doing something like shelling out a text file into Notepad. Doing so allows me to control the appearance of the application, the caption in the title bar of the form, etc.
Figure 1: The Editor Application in Use Getting StartedIn order to get started, unzip the attachment and load the solution into Visual Studio 2005. Examine the solution explorer and note the files contained in the project:
Figure 2: The Solution Explorer Showing the Project Files The solution contains the editor application "RichTextEditor". This application uses an extended rich text box control built by Microsoft and then to that adds in all of the normal document manipulation techniques (such as font selection, indentation, and file IO support). The Microsoft extension to the RichTextBox control is added as a reference to the project; the DLL is included with the download. Aside from the main application's form (frmMain.cs), this project also includes two additional forms, one to search for a string within the RTB's content, and one to search for and replace strings within the RTB. These forms are frmFind.cs and frmReplace.cs.
Figure 2: Editor Application Find Dialog
Figure 3: Editor Application Find and Replace Dialog In addition to the form classes mentioned, the solution also contains a folder entitled, "Graphics"; this folder contains a collection of image files that may be used to support the display of icons on application's menus and toolbar. Project ReferencesAside from the default references, there are a couple of additional references added. Most notably, the extended rich text box control library is added to allow for the use of the extended rich text box control. Figure 4 shows the references as they exist in the project.
Figure 4: Project References The Code: Main Form ClassThe main form class (frmMain.cs) is pretty easy to follow. It begins with a couple of import statements followed by the class declaration. The code is divided by purpose into four separate regions which are:
The imports, namespace, and class declaration sections of code look like this: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using ExtendedRichTextBox;
namespace RichTextEditor
{
public partial class frmMain : Form
{
The class declaration is plain enough and merely inherits the The constructor is pretty simple; it is used to set the text property of the form and to set the default file name to an empty string: // constructor
public frmMain()
{
InitializeComponent();
currentFile = "";
this.Text = "Editor:New Document";
}
The declarations section is also very simple; the section contains both of the variables used throughout the application. The content is as follows: #region Declaration
private string currentFile;
private int checkPrint;
#endregion
The The The next section of code is the primary block of code used by this application, the The first block of code in the menu region is used to create a new file: #region Menu Methods
private void NewToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (rtbDoc.Modified == true)
{
System.Windows.Forms.DialogResult answer;
answer = MessageBox.Show("Save current document before
creating new document?", "Unsaved Document",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == System.Windows.Forms.DialogResult.No)
{
currentFile = "";
this.Text = "Editor: New Document";
rtbDoc.Modified = false;
rtbDoc.Clear();
return;
}
else
{
SaveToolStripMenuItem_Click(this, new EventArgs());
rtbDoc.Modified = false;
rtbDoc.Clear();
currentFile = "";
this.Text = "Editor: New Document";
return;
}
}
else
{
currentFile = "";
this.Text = "Editor: New Document";
rtbDoc.Modified = false;
rtbDoc.Clear();
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
In this method, prior to clearing the current document, the status of the current document is checked by means using the controls
Figure 5: Confirmation Dialog show after user requests new document with saving changes. The next method is used to open an existing file into the rich text box control: private void OpenToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (rtbDoc.Modified == true)
{
System.Windows.Forms.DialogResult answer;
answer = MessageBox.Show("Save current file before opening
another document?", "Unsaved Document",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == System.Windows.Forms.DialogResult.No)
{
rtbDoc.Modified = false;
OpenFile();
}
else
{
SaveToolStripMenuItem_Click(this, new EventArgs());
OpenFile();
}
}
else
{
OpenFile();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
This method works in a manner similar to the "new" method discussed in the previous section. If the file has not been modified or if the user decides not to save the current modifications, the method calls an private void OpenFile()
{
try
{
OpenFileDialog1.Title = "RTE - Open File";
OpenFileDialog1.DefaultExt = "rtf";
OpenFileDialog1.Filter = "Rich Text Files|*.rtf|Text
Files|*.txt|HTML Files|*.htm|All Files|*.*";
OpenFileDialog1.FilterIndex = 1;
OpenFileDialog1.FileName = string.Empty;
if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
{
if (OpenFileDialog1.FileName == "")
{
return;
}
string strExt;
strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName);
strExt = strExt.ToUpper();
if (strExt == ".RTF")
{
rtbDoc.LoadFile(OpenFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}
else
{
System.IO.StreamReader txtReader;
txtReader = new
System.IO.StreamReader(OpenFileDialog1.FileName);
rtbDoc.Text = txtReader.ReadToEnd();
txtReader.Close();
txtReader = null;
rtbDoc.SelectionStart = 0;
rtbDoc.SelectionLength = 0;
}
currentFile = OpenFileDialog1.FileName;
rtbDoc.Modified = false;
this.Text = "Editor: " + currentFile.ToString();
}
else
{
MessageBox.Show("Open File request cancelled by user.",
"Cancelled");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 6: Open File Dialog The first part of the File Open method is used to configure and display the Open File Dialog box; this code sets up the filters for the files types (rich text, text, or html), sets the dialog box title, and a selects an extension filter (RTF). Once this is done, the dialog box is displayed, if the user accepts the dialog box without specifying a file name, the method will exit. If a file name exists, the code extracts the file extension from the file name, places the extension in a local string variable, converts it to upper case and then checks to see if the file is an RTF file or another type of file. If the file selected by the user is an RTF file, the method calls the control's Load File method and passes it the file name and format. If the file is not an RTF file, a stream reader is instanced and passed the path to the file, the control is then fed the content of the file by means of the stream reader's read to end method. The reader is then closed and disposed of and the cursor is moved to the beginning of the document and the selection length is set to zero (if you do not do this, the whole document will initialize as entirely selected). After the file is loaded, the The next item up is the private void SaveToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (currentFile == string.Empty)
{
SaveAsToolStripMenuItem_Click(this, e);
return;
}
try
{
string strExt;
strExt = System.IO.Path.GetExtension(currentFile);
strExt = strExt.ToUpper();
if (strExt == ".RTF")
{
rtbDoc.SaveFile(currentFile);
}
else
{
System.IO.StreamWriter txtWriter;
txtWriter = new System.IO.StreamWriter(currentFile);
txtWriter.Write(rtbDoc.Text);
txtWriter.Close();
txtWriter = null;
rtbDoc.SelectionStart = 0;
rtbDoc.SelectionLength = 0;
}
this.Text = "Editor: " + currentFile.ToString();
rtbDoc.Modified = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "File Save
Error");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The With both the file open and file save methods used, if the file is anything other than a rich text file, the application will attempt to open or save it with a stream reader or stream writer; this will allow the application to work with any text file, not just RTF, TXT, or HTML extensions. That would permit you to use it with custom file types or other things such as license files or error log files. The next method addresses the private void SaveAsToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
SaveFileDialog1.Title = "RTE - Save File";
SaveFileDialog1.DefaultExt = "rtf";
SaveFileDialog1.Filter = "Rich Text Files|*.rtf|Text
Files|*.txt|HTML Files|*.htm|All Files|*.*";
SaveFileDialog1.FilterIndex = 1;
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
if (SaveFileDialog1.FileName == "")
{
return;
}
string strExt;
strExt = System.IO.Path.GetExtension(SaveFileDialog1.FileName);
strExt = strExt.ToUpper();
if (strExt == ".RTF")
{
rtbDoc.SaveFile(SaveFileDialog1.FileName,
RichTextBoxStreamType.RichText);
}
else
{
System.IO.StreamWriter txtWriter;
txtWriter = new
System.IO.StreamWriter(SaveFileDialog1.FileName);
txtWriter.Write(rtbDoc.Text);
txtWriter.Close();
txtWriter = null;
rtbDoc.SelectionStart = 0;
rtbDoc.SelectionLength = 0;
}
currentFile = SaveFileDialog1.FileName;
rtbDoc.Modified = false;
this.Text = "Editor: " + currentFile.ToString();
MessageBox.Show(currentFile.ToString() + " saved.",
"File Save");
}
else
{
MessageBox.Show("Save File request cancelled by user.",
"Cancelled");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 7: File Menu Options By now this should look pretty familiar; in the code, a The next item in the code is the menu's private void ExitToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (rtbDoc.Modified == true)
{
System.Windows.Forms.DialogResult answer;
answer = MessageBox.Show("Save this document before
closing?", "Unsaved Document",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (answer == System.Windows.Forms.DialogResult.Yes)
{
return;
}
else
{
rtbDoc.Modified = false;
Application.Exit();
}
}
else
{
rtbDoc.Modified = false;
Application.Exit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The next menu option addressed is the edit menu's private void SelectAllToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
rtbDoc.SelectAll();
}
catch (Exception)
{
MessageBox.Show("Unable to select all document content.",
"RTE - Select", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Following the edit menu's
Figure 8: Edit Menu Options Next up is the menu option used to select the current font. This code merely uses a standard font dialog box to set the rich text box control's selection font property to the font selected by the user through the font dialog. The code used to do this is as follows: private void SelectFontToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
if (!(rtbDoc.SelectionFont == null))
{
FontDialog1.Font = rtbDoc.SelectionFont;
}
else
{
FontDialog1.Font = null;
}
FontDialog1.ShowApply = true;
if (FontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
rtbDoc.SelectionFont = FontDialog1.Font;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 9: Font Dialog in Use Similarly the font color menu option is used to display a standard color dialog to the user; if the user selects a color from the dialog, the foreground color property of the document will be updated to contain the selected color. As the rich text box control works primarily with selected text (that is, what you change in terms of selecting a font or changing a color), this function will alter the color only of the selected text. If no text is selected, the color at the insertion point will hold the foreground color selection and typing from the insertion point will show text in the newly selected color. private void FontColorToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
ColorDialog1.Color = rtbDoc.ForeColor;
if (ColorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
rtbDoc.SelectionColor = ColorDialog1.Color;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The next three sections of code are the methods used to set the selected text's bold, italic, or underline properties. Each section is set up to work such that, if the selected text is bold, selecting the bold option will remove the bolding (or italics, or underline). (As they all basically work the same, I am only showing bold here) private void BoldToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (!(rtbDoc.SelectionFont == null))
{
System.Drawing.Font currentFont = rtbDoc.SelectionFont;
System.Drawing.FontStyle newFontStyle;
newFontStyle = rtbDoc.SelectionFont.Style ^
FontStyle.Bold;
rtbDoc.SelectionFont = new Font(currentFont.FontFamily,
currentFont.Size, newFontStyle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The private void NormalToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
if (!(rtbDoc.SelectionFont == null))
{
System.Drawing.Font currentFont = rtbDoc.SelectionFont;
System.Drawing.FontStyle newFontStyle;
newFontStyle = FontStyle.Regular;
rtbDoc.SelectionFont = new Font(currentFont.FontFamily,
currentFont.Size, newFontStyle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The menu option used to set the page color is used to expose a color dialog box to the user; if the user selects a color form the dialog, the back color of the rich text box control is set to that color. The code to support this function is as follows: private void PageColorToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
ColorDialog1.Color = rtbDoc.BackColor;
if (ColorDialog1.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
rtbDoc.BackColor = ColorDialog1.Color;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 10: Color Dialog in Use The private void mnuUndo_Click(object sender, System.EventArgs e)
{
try
{
if (rtbDoc.CanUndo)
{
rtbDoc.Undo();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void mnuRedo_Click(object sender, System.EventArgs e)
{
try
{
if (rtbDoc.CanRedo)
{
rtbDoc.Redo();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The next three sections of code address setting the document's horizontal alignment property to support left, centered, or right justification of the selected text. Each of these alignment control options is directly supported by the control: private void LeftToolStripMenuItem_Click_1(object sender, System.EventArgs e)
{
try
{
rtbDoc.SelectionAlignment = HorizontalAlignment.Left;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void CenterToolStripMenuItem_Click_1(object sender,
System.EventArgs e)
{
try
{
rtbDoc.SelectionAlignment = HorizontalAlignment.Center;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void RightToolStripMenuItem_Click_1(object sender,
System.EventArgs e)
{
try
{
rtbDoc.SelectionAlignment = HorizontalAlignment.Right;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 11: Alignment Options Adding and removing bullets is also directly supported by the control, the private void AddBulletsToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
rtbDoc.BulletIndent = 10;
rtbDoc.SelectionBullet = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void RemoveBulletsToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
rtbDoc.SelectionBullet = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 12: Bullet Options Setting the indentation level for the selected text is also directly supported by the control: private void mnuIndent0_Click(object sender, System.EventArgs e)
{
try
{
rtbDoc.SelectionIndent = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Figure 13: Indentation Options The application contains a separate dialog box used to find a text string within the current document; if the user selects the find menu option, the application will create and display a new instance of the search form (frmFind.cs): private void FindToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
frmFind f = new frmFind(this);
f.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
Similarly, if the user selects the find and replace menu option, the application will create and display a new instance of the find and replace form (frmReplace.cs): private void FindAndReplaceToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
frmReplace f = new frmReplace(this);
f.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The print document is set to contain the content of the current rich text box control as the print document; in order to support print preview, page setup, and printing, given the modifications made to the extended rich text box control, all that needs to be done is to pass the print document to each of the related standard dialog boxes: private void PreviewToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
try
{
PrintPreviewDialog1.Document = PrintDocument1;
PrintPreviewDialog1.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void PrintToolStripMenuItem_Click(object sender, System.EventArgs e)
{
try
{
PrintDialog1.Document = PrintDocument1;
if (PrintDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
PrintDocument1.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
private void mnuPageSetup_Click(object sender, System.EventArgs e)
{
try
{
PageSetupDialog1.Document = PrintDocument1;
PageSetupDialog1.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The next method is a little more interesting; it is used to embed an image file into the extended rich text box document. This method uses an open file dialog box set to filter for the extensions used for bitmaps, jpegs, and gif files. The user may navigate to the file that they want to embed into the document (placing it at the insertion point defined by the cursor). private void InsertImageToolStripMenuItem_Click(object sender,
System.EventArgs e)
{
OpenFileDialog1.Title = "RTE - Insert Image File";
OpenFileDialog1.DefaultExt = "rtf";
OpenFileDialog1.Filter = "Bitmap Files|*.bmp|JPEG Files|*.jpg|GIF
Files|*.gif";
OpenFileDialog1.FilterIndex = 1;
OpenFileDialog1.ShowDialog();
if (OpenFileDialog1.FileName == "")
{
return;
}
try
{
string strImagePath = OpenFileDialog1.FileName;
Image img;
img = Image.FromFile(strImagePath);
Clipboard.SetDataObject(img);
DataFormats.Format df;
df = DataFormats.GetFormat(DataFormats.Bitmap);
if (this.rtbDoc.CanPaste(df))
{
this.rtbDoc.Paste(df);
}
}
catch
{
MessageBox.Show("Unable to insert image format selected.",
"RTE - Paste", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
Figure 14: Embedding an Image File Once the user selects a file through the open file dialog, the method will create an image using the The next bit of code in the application is used to update the toolbar icons whenever the selection changes in the rich text box control; this is used to keep the selected state of the toolbar buttons straight depending upon what is selected in the control. #region
Toolbar Methods
private void tbrSave_Click(object sender, System.EventArgs e)
{
SaveToolStripMenuItem_Click(this, e);
}
…
The last section of the main form's code is contained in the printing region. The calls used to print, due to the use of the extended rich text box control are very simple: private void PrintDocument1_BeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
checkPrint = 0;
}
The The private void PrintDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
checkPrint = rtbDoc.Print(checkPrint, rtbDoc.TextLength, e);
if (checkPrint < rtbDoc.TextLength)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
Figure 15: Print Preview The print section wraps up the rest of the main form class. Code: The Find and Replace FormThe find and replace form is supported with the frmReplace.cs class. The find class is defined in frmFind.cs but since it contains two methods (find and find next) which are also contained in the find and replace form, I will only discuss the code contained in the find and replace form. The find and replace form supports four methods:
The find method is pretty straightforward; it will search the entire document for the first occurrence of the search term defined by the user on the form. It will search in one of two ways: with or without matching the case of the search term. Depending upon whether or not the user has checked the Match Case check box on the form, the application will search for the text using either the private void btnFind_Click(object sender, System.EventArgs e)
{
try
{
int StartPosition;
StringComparison SearchType;
if (chkMatchCase.Checked == true)
{
SearchType = StringComparison.Ordinal;
}
else
{
SearchType = StringComparison.OrdinalIgnoreCase;
}
StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text,
SearchType);
if (StartPosition == 0)
{
MessageBox.Show("String: " +
txtSearchTerm.Text.ToString() + " not found",
"No Matches", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
return;
}
mMain.rtbDoc.Select(StartPosition, txtSearchTerm.Text.Length);
mMain.rtbDoc.ScrollToCaret();
mMain.Focus();
btnFindNext.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The find next function works in a manner consistent with the find function; the only difference is that it sets the start position to the current position of the selection starting point within the document so that the find next function will not start at the beginning of the document each time it searches: private void btnFindNext_Click(object sender, System.EventArgs e)
{
try
{
int StartPosition = mMain.rtbDoc.SelectionStart + 2;
StringComparison SearchType;
if (chkMatchCase.Checked == true)
{
SearchType = StringComparison.Ordinal;
}
else
{
SearchType = StringComparison.OrdinalIgnoreCase;
}
StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text,
StartPosition, SearchType);
if (StartPosition == 0 || StartPosition < 0)
{
MessageBox.Show("String: " +
txtSearchTerm.Text.ToString() + " not found",
"No Matches", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
return;
}
mMain.rtbDoc.Select(StartPosition,
txtSearchTerm.Text.Length);
mMain.rtbDoc.ScrollToCaret();
mMain.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The private void btnReplace_Click(object sender, System.EventArgs e)
{
try
{
if (mMain.rtbDoc.SelectedText.Length != 0)
{
mMain.rtbDoc.SelectedText = txtReplacementText.Text;
}
int StartPosition;
StringComparison SearchType;
if (chkMatchCase.Checked == true)
{
SearchType = StringComparison.Ordinal;
}
else
{
SearchType = StringComparison.OrdinalIgnoreCase;
}
StartPosition = mMain.rtbDoc.Text.IndexOf(txtSearchTerm.Text,
SearchType);
if (StartPosition == 0 || StartPosition < 0)
{
MessageBox.Show("String: " +
txtSearchTerm.Text.ToString() + " not found",
"No Matches", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk);
return;
}
mMain.rtbDoc.Select(StartPosition, txtSearchTerm.Text.Length);
mMain.rtbDoc.ScrollToCaret();
mMain.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Error");
}
}
The private void btnReplaceAll_Click(object sender, System.EventArgs e)
{
try
{
mMain.rtbDoc.Rtf = Main.rtbDoc.Rtf.Replace(txtSearchTerm.Text.Trim(),
txtReplacementText.Text.Trim());
int StartPosition;
StringComparison SearchType;
if (chkMatchCase.Checked == true)
{
SearchType = StringComparison.Ordinal;
}
else
{
SearchType = StringComparison.OrdinalIgnoreCase;
}
StartPosition = mMain.rtbDoc.Text.IndexOf(txtReplacementText.Text,
SearchType);
mMain.rtbDoc.Select(StartPosition,
txtReplacementText.Text.Length);
mMain.rtbDoc.ScrollToCaret();
mMain.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error");
}
}
Again, the frmFind.cs class is the same as the replace class with the exception being that it does not support the replace and replace all methods. Code: Rich Text Box Print ControlThe class library contained in the ExtendedRichTextBox.dll was developed at Microsoft; for the purposes of the project, I merely compiled the Microsoft code into the DLL. For a complete description of the content of the class, please refer to this link in your Visual Studio 2005 help files: ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.KB.v10.en/
enu_kbvbnetkb/vbnetkb/811401.htm
SummaryThis article and sample application have attempted to demonstrate some of the available techniques useful in creating and managing text and text files through the use of the rich text box control. The control itself was modified using an approach recommended by Microsoft to greatly facilitate the ease with which one may print the contents of the text or rich text file. Further, the application provided an approach to inserting an image into the rich text box as a means of creating a more useful application based upon the rich text box control.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||