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

RichTextBox Control with Find functionality

By , 22 Jan 2009
 

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)

About the Author

Ashutosh Phoujdar
Technical Lead Infosys Technologies Ltd.
India India
Member
He has done Masters of Computer Application from Gautam Buddh Technical University Lucknow, India. He is having work experience of 6 years in mainly Microsoft Technologies and Open source framework. Currently working as Product Technical Lead in Infosys Technologies Ltd. Products R&D division. Earlier to this worked as Senior Software Developer with Ness Software Products Lab. He started his career with PASCAL, then moved to C and finally got into VB 5.0 and now enjoys programming into C#. He loves to works on Enterprise software development using ASP.NET, MOSS and SQL Server. Withal he is proficient in development and System Integration. He is advocate of Open source and love to share solutions with open source communities like nHibernate, SubSonic, SourceForge.
 
Award's :
Prize winner in Competition "Best VB.NET article of May 2008"
 
Ashu' Articles :
Click to see my CodeProject Articles
 
Ashu's Blog :
Share a solution | Explore the .NET world
 
Ashu's Favorite :
nHibernate - The best ORM.
nHibernate Contributed Tools
SubSonic - Best Auto generated DAL.
 

Click to send me your wishes :ashufouzdar@.in.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank youmemberKumar Shanmugam16 Feb '10 - 17:55 
QuestionCan I use RichTextBox control as a word Document ViewermemberRAJEEV_RSD12 Sep '09 - 15:18 
AnswerRe: Can I use RichTextBox control as a word Document Viewermemberdnpro12 Sep '09 - 21:40 
Questionhow can I make this work in vb6?memberACPR48318 Feb '09 - 10:06 
GeneralMy vote of 1memberKenny McKee31 Jan '09 - 8:12 
GeneralMy vote of 1memberGriffinPeter23 Jan '09 - 0:46 
GeneralRe: My vote of 1memberashu fouzdar25 Jan '09 - 0:05 
GeneralVote of 1memberGriffinPeter23 Jan '09 - 0:45 
GeneralRe: Vote of 1memberdnpro27 Jan '09 - 0:34 
General[Message Deleted]memberAshishJoshi-IT Expert22 Jan '09 - 20:16 
GeneralRe: Really nice article dat helped me a lotmemberashu fouzdar22 Jan '09 - 21:10 

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

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