Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic

RichTextBox Control with Find functionality

Rate me:
Please Sign up or sign in to vote.
3.77/5 (8 votes)
22 Jan 2009CPOL4 min read 75.1K   3.5K   20   14
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.

VB.NET
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 :

VB.NET
'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.

VB.NET
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 :

VB.NET
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)


Written By
Architect
Canada Canada
Ashutosh is an avid programmer, who “lives to code”. One can always find him busy seeking challenging programming assignments on various complex problems ranging from Data management, Classification, Optimization, Security, Network analysis, Distributed computing.

He started his programming stint with “C”; he has also worked on C++, Visual Basic, JAVA, Perl, FoxPro, PASCAL, Shell Scripting, and Perl. Currently, he is proficient and working on C#.

His area of interest includes Distributed Computing, Analytic and Business Intelligence, Large Enterprise System Architectures, Enterprise Content Management.

He is an advocate of Open source and likes 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

Blog :
Share a solution | Explore the .NET world

Link'd :


His Favorite(s) :
nHibernate - The best ORM.
nHibernate Contributed Tools

Comments and Discussions

 
QuestionAnother same code Pin
Member 1400366422-Nov-18 6:11
Member 1400366422-Nov-18 6:11 
QuestionDownloaded DLL seems to be empty Pin
mastrocodeproj27-Aug-14 11:21
mastrocodeproj27-Aug-14 11:21 
GeneralMy vote of 5 Pin
aaroncampf9-May-14 8:56
aaroncampf9-May-14 8:56 
GeneralThank you Pin
Kumar Shanmugam16-Feb-10 17:55
professionalKumar Shanmugam16-Feb-10 17:55 
QuestionCan I use RichTextBox control as a word Document Viewer Pin
RAJEEV_RSD12-Sep-09 15:18
RAJEEV_RSD12-Sep-09 15:18 
AnswerRe: Can I use RichTextBox control as a word Document Viewer Pin
Ashutosh Phoujdar12-Sep-09 21:40
Ashutosh Phoujdar12-Sep-09 21:40 
Questionhow can I make this work in vb6? Pin
ACPR48318-Feb-09 10:06
ACPR48318-Feb-09 10:06 
GeneralMy vote of 1 Pin
Kenny McKee31-Jan-09 8:12
Kenny McKee31-Jan-09 8:12 
GeneralMy vote of 1 Pin
CARPETBURNER23-Jan-09 0:46
CARPETBURNER23-Jan-09 0:46 
GeneralRe: My vote of 1 Pin
Ashutosh Phoujdar25-Jan-09 0:05
Ashutosh Phoujdar25-Jan-09 0:05 
GeneralVote of 1 Pin
CARPETBURNER23-Jan-09 0:45
CARPETBURNER23-Jan-09 0:45 
GeneralRe: Vote of 1 Pin
Ashutosh Phoujdar27-Jan-09 0:34
Ashutosh Phoujdar27-Jan-09 0:34 
General[Message Deleted] Pin
AshishJoshi-IT Expert22-Jan-09 20:16
AshishJoshi-IT Expert22-Jan-09 20:16 
GeneralRe: Really nice article dat helped me a lot Pin
Ashutosh Phoujdar22-Jan-09 21:10
Ashutosh Phoujdar22-Jan-09 21:10 

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

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