Searching for text in a RichTextBox control

mivey4

Member
Joined
Mar 29, 2005
Messages
11
Programming Experience
1-3
I need help trying to use the FindReplace Dialog box unless there is another way to do this. I have constructed the following code to search the text in a textbox control field and it works well. Below is the code I am using. The example is using 1 button (Button1) and 1 textbox (Textbox1):

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' Specify the Type and all the Show method of the FindReplaceDialog.

ctrlFindReplace.Type = FindReplace.FindReplaceDialogType.Find
ctrlFindReplace.Show(
Me)
EndSub
PrivateSub ctrlFindReplace_FindNextClick() Handles ctrlFindReplace.FindNextClick

' Handles FindNext event raised by the FindReplaceDialog. I have simplified the code here from a Microsoft sample.

Dim txtBox As System.Windows.Forms.TextBox
ActiveControl = Me.ActiveControl

IfTypeOf ActiveControl Is System.Windows.Forms.TextBox Then
TextBox1 = CType(ActiveControl, System.Windows.Forms.TextBox)
'Find the string typed in the textbox
ctrlFindReplace.FindString(TextBox1)
EndIf
EndSub


This code actually does precisely what I want it to do. It shows the dialog control, allows me to enter a text string, and automatically selects the next occurence of the string everytime the Find Next button is clicked.

Now the problem, I need this to work with a richTextBox control, not a simple textbox but the argument for the FindString method will only accept a textbox control source.


I can't believe that as powerful and versatile as VB.NET is that it has such a trivial restriction. So I must be doing something wrong. I know this dialog control has been used with MS Word which supports more than a simple text format.


Can anyone help me please??? I have tried other forums but can't seem to get an answer.
 
Hello Zack,

Actually, ctrlFindReplace is declared as an instance of the FindReplace dialog box of the Microsoft Common Dialog controls class. Any ideas on how to make it work using the RichTextBox control?

thanks!
 
Erh... Nope
Can you please guide me through or do you have any samples to provide?
Sorry about the inconveniece caused.
 
for finding a text in a richtextbox

you use the instr function of the vbscript

example: instr(<starting number>,<original string>,<string to find>,vbtextcompare/vbbinarycompare)

if this return 0 then it means string has been found.

<starting number> means the starting index of th elenght of a tsring from where to start the finding process.
 
Thanks nakracreative for the suggestion, but I really wanted to use the findDialog to locate the text in the RichTextBox. I know of the method you're speaking of and I have tried that but it is somewhat slow.

It seems as though the FindDialog control is much faster in locating text strings especially when the number of words in the control very long. I need to be able to search extremely large log files and the larger the files the longer your recommended method takes to locate the string.

Maybe its because the FindDialog seems to be faster because it actually runs as a separate object and locates the string with its own resources placing focus back to the text control when the finds the match. Where as your method actually utilizes the richtextcontrols methods. Just seems to be slower for some reason.

I guess no one has an answer for this question.

I appreciate your reply though!
 
Thanks everyone for the assistance. The RTFPad.zip sample provided by kulrom resolved my problem and provided me the solution I was looking for. The link provided by Jim in the valley also was a great insight on the code provided in his link as well.

Much thanks!
Cheers.
 
Back
Top