Click here to Skip to main content
Click here to Skip to main content

RichTextBox Control with Find functionality

, 22 Jan 2009
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ashutosh Phoujdar
Architect Infosys Technologies Ltd.
India India
An Software Architect having work experience of 8+ years in Architecture, Design and Development of Enterprise class application for Analytic s and Business Intelligence. Having hands-on experience primarily in Microsoft Technology Stack including(but not limited to) .NET, SQL Server, SharePoint Server, Biztalk, Withal know to program in Java, Perl and Shell. Currently working as Product Technical Architect in Infosys Technologies Ltd. Products R&D division. He has worked on development of best in class Supply Chain Optimization System and Spend Classification Software for optimizing indirect spend. An avid programmer by heart and passionate for solving any challenging and complex business problems. He is advocate of Open source and love to share solutions with open source communities like
1.Stack Overflow
2. nHibernate
.
 
Award's :
Prize winner in Competition "Best article of May 2008"
 
Articles :
Click to see my CodeProject Articles
 
Open Source Contributions:
Click to see my Open Source Project
 
Blog :
Share a solution | Explore the .NET world
 
Link'd :
LinkedIn Profile
 
His Favorite(s) :
nHibernate - The best ORM.
nHibernate Contributed Tools
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionDownloaded DLL seems to be empty Pinmembermastrocodeproj27-Aug-14 11:21 
GeneralMy vote of 5 Pinmemberaaroncampf9-May-14 8:56 
GeneralThank you PinmemberKumar Shanmugam16-Feb-10 17:55 
QuestionCan I use RichTextBox control as a word Document Viewer PinmemberRAJEEV_RSD12-Sep-09 15:18 
AnswerRe: Can I use RichTextBox control as a word Document Viewer Pinmemberdnpro12-Sep-09 21:40 
Questionhow can I make this work in vb6? PinmemberACPR48318-Feb-09 10:06 
GeneralMy vote of 1 PinmemberKenny McKee31-Jan-09 8:12 
GeneralMy vote of 1 PinmemberGriffinPeter23-Jan-09 0:46 
GeneralRe: My vote of 1 Pinmemberashu fouzdar25-Jan-09 0:05 
GeneralVote of 1 PinmemberGriffinPeter23-Jan-09 0:45 
GeneralRe: Vote of 1 Pinmemberdnpro27-Jan-09 0:34 
General[Message Deleted] PinmemberAshishJoshi-IT Expert22-Jan-09 20:16 
GeneralRe: Really nice article dat helped me a lot Pinmemberashu fouzdar22-Jan-09 21:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 23 Jan 2009
Article Copyright 2009 by Ashutosh Phoujdar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid