Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

RichTextBox Control with Find functionality

0.00/5 (No votes)
22 Jan 2009 1  
A UserControl derived from RichTextBox control to support Find feature

findBox.png

Introduction

After posting my last article (LogViewer - A Simple log listening log utility), I received some remarks as well few request to provide some enhancement for custom needs. Although It was nice experience of implementing those new enhancements, I came up with idea of new utility control that encapsulate a richtext box inside and provide some feature over it.

The Problem

Most of my work involves development of WinForm based application, as part of work I have major interactions with common window controls especially RichTextBox. I am using this control since old VB eras with richtx32.ocx. It is very perfect control for displaying as well manipulating text in RTF as well simple text format. As I mentioned earlier that I came up with idea enhancing richtextbox control, Well the reason behind that is very simple. I had to implement Find dialog box that need to be attached with shortcut key CTRL+F. Although it sounds very simple, but I was not able to find any straight way to accomplish this task with only richtextbox control.

This is the only reason to implement another user control that inherits all functionality
of a richtextbox as well provide feature to perform search based on different criterions :

1. Search within part of word/line.
2. Case sensitive and Case insensitive search.
3. Whole word search.
4. Search in forward and backword direction.

Solution

The problems of searching within richtextbox content can be solved easily by using this enhanced richtextbox control. It works same as normal richtextbox, It retains all functionality as well provide find feature to search content of richtextbox.

How it works

1.Enhanced RichTextBox control inherits from standard richtextbox control.

Partial Class RichTextBoxEnh
Inherits System.Windows.Forms.RichTextBox
'Implementation code
End Class

2.It declared 3 members for tracking find position, find criteria and to display find dialog box :

'This is instance of find dialog box, used to display find dialog box.
Private WithEvents findDialog As DlgFind
'This integer instance is used to track last found position of the word.
Private foundIndex As Integer 
'This string instance is used to track which was the last word found.
Private foundWord As String 

3. Then it intercept KeyUp event, for catching CTRL + F key combination.

Private Sub RichTextBoxEnh_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyUp
    If e.Modifiers = Keys.Control And e.KeyCode = Keys.F Then
    If findDialog Is Nothing Then findDialog = New DlgFind()
    Me.HideSelection = False
    findDialog.ShowDialog()
    End If
End Sub



The above event handler intercepts CTRL + F key combination and create new instance of DlgFind class and show the dialog to user for providing search criterions i.e. search string, find direction, case senstivity.

                             findDlg.PNG

When user clicks Find Next to start the find operation, DlgFind instance raises Find event, which is handled by our richtextbox instance and used to carry out search operation :

Private Sub findDialog_Find(ByVal findWhat As String, ByVal findOption As RichTextBoxFinds) _
Handles findDialog.Find

Dim findIndex As Integer = 0
If findWhat.Equals(foundWord) Then findIndex = foundIndex
If findOption And RichTextBoxFinds.Reverse = RichTextBoxFinds.Reverse Then
findIndex = Me.Find(findWhat, 0, findIndex, findOption)
Else
findIndex = Me.Find(findWhat, findIndex, findOption)
End If
If findIndex > 0 Then
foundWord = findWhat
If findOption And RichTextBoxFinds.Reverse = RichTextBoxFinds.Reverse Then
foundIndex = findIndex
Else
foundIndex = findIndex + findWhat.Length
End If
End If

End Sub.

The above event handler evaluate the search direction and invoke find operation on richtextbox
using last found index (in case of same string searched again) otherwise starts from 0.
In case of successfull find operation it keep track of found position as well found string by
assigning it to foundIndex and foundWord member variable.

The problems of searching within richtextbox content can be solved easily by using this enhanced richtextbox control. It works same as normal richtextbox, It retains all functionality as well provide find feature to search content of richtextbox.

 

Sequence diagram for Find operation

seq.png 

 

Summary of mode of operation

1. A Find dialog box is used to accept various search criterions from user.
2. When user clicks FindNext button or press enter the dialog box raises find event.
3. The find event passes string to be find as well other search options e.g. MatchCase,
SearchDirection, Match Whole Word.
4. This find event is intercepted by richtextbox control and used to perform search
based on search criteria provided.
5. Additionally it uses instance specific variables to retain last found string
and position index to resume search from last found position
(I guess this is identical behavior, If not please change according to will).

How To Use

For using this control, you need to add this control to toolbox. To add the control to Form Designer's toolbox, use following steps :

1. Download zip file containing binary(*.dll) for control.
2. Right Click ToolBox and Select Choose Item from Context menu

rclick.png
3. Click browse and locate the dll for control.

browse.png
4. Click Ok, Now the control will be available in toolbox.

ico.png


5. Drag the control from toolbox to form.


Well thats all folks.

Please forward your suggestions to me, and please vote this article If you like this utility.

History

Initial Revision 1.0 - 23 Jan 2009

Updated with Code snippets and more explanation 1.1 - 27 Jan 2009

Disclaimer

THIS UTILITY CONTROL IS NOT FOLLOWING ANY STANDARD DESIGN PATTERN AND PROVIDED BY
THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here