Some help with finding text

Budda

Member
Joined
Dec 16, 2006
Messages
9
Programming Experience
Beginner
I have made my own simple find text dialog, to link with my richtextbox. Currently it works, but every time it only selects the first appearance of the wanted text, and stops there.

I would like it to stay open, and highlight the wanted text from the textbox in the dialog without having to press cancel after OK. (Without the me.close() I have to press OK then Cancel for it to find and highlight the text.

I also want it to find text forward from where the cursor currently is. Like most of Windows Find Dialogs, and make it stay open so you can switch through all the text wanted in the richtextbox.

Basically, I want pretty much a Find Dialog the same as most Windows Programs.
Currently I have this:
VB.NET:
[SIZE=2][COLOR=#0000ff]
Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] FindDialog
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OK_Button_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] OK_Button.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DialogResult = System.Windows.Forms.DialogResult.OK
[/SIZE][SIZE=2][COLOR=#008000]'If Developer.RichTextBox1.SelectedText Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'End If
[/COLOR][/SIZE][SIZE=2]Developer.RichTextBox1.Find(TextBox1.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Cancel_Button_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Cancel_Button.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DialogResult = System.Windows.Forms.DialogResult.Cancel
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]

Help Please.

P.S. Why doesn't VB have Find Dialogs built in? Or is there an add-in somewhere where I can get this.
 
There are 7 overloads for the RichTextBox.Find function method to be used with various input parameters, they all return the index of the found occurance or -1 if not found. Investigate the RichTextBox class documentation, with the index and known text length (= the text being searched for) you can make the selection with one of the many RichTextBox members that start with "Select...". You can also in case you are searching/finding a partial word make a selection of 0 length just to move the attention to that spot, or if you want search for a word delimiter starting from the match index and make the full word selection afterwards.
 
One that meets your requirement of searching from a specified index, you have several options.
 
Alright, I think I have the one I want. Thanks. However, in the function, one of the parts is:
VB.NET:
[SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] start [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
Which indicates the start of the search. I want this to be where the cursor is in the RichTextBox, but when I enter:
VB.NET:
[SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] Developer.FindMyText(TextBox1.Text, Developer.RichTextBox1.[B]Cursor[/B])[/SIZE]
It doesn't allow it to work, saying "Cannot convert Developer.RichTextBox1.Cursor to Integer" - as it does -_-.
I've looked through all the other possiblilities I can find, but I know this is the one I need, yet it won't let me.

Any ideas?
 
Well that worked I think, but not fully. I found a code on the net for a find and replace dialog although there is one problem.

When I tested the program, the Find and Replace part, when I click 'Find' or 'Find Next', it does everything correctly, except highlighting the text. It zooms to the text, but nothing more. I checked the code, and it has the RichTextBox.Select property in it, but it doesn't seem to work.

The code for the find and find next are as follows:
Find:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnFind_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnFind.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartPosition [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SearchType [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CompareMethod
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] chkMatchCase.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]SearchType = CompareMethod.Binary
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]SearchType = CompareMethod.Text
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]StartPosition = InStr(1, Developer.RichTextBox1.Text, _
txtSearchTerm.Text, SearchType)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] StartPosition = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"String: '"[/COLOR][/SIZE][SIZE=2] & txtSearchTerm.Text.ToString() & _
[/SIZE][SIZE=2][COLOR=#800000]"' not found"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"No Matches"[/COLOR][/SIZE][SIZE=2], _
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
[/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]Developer.RichTextBox1.Select(StartPosition - 1, txtSearchTerm.Text.Length)
Developer.RichTextBox1.ScrollToCaret()
Developer.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
Find Next:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] BtnFindNext_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] BtnFindNext.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] StartPosition [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = Developer.RichTextBox1.SelectionStart + 2
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] SearchType [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] CompareMethod
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] chkMatchCase.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]SearchType = CompareMethod.Binary
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]SearchType = CompareMethod.Text
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]StartPosition = InStr(StartPosition, Developer.RichTextBox1.Text, _
txtSearchTerm.Text, SearchType)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] StartPosition = 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"String: '"[/COLOR][/SIZE][SIZE=2] & txtSearchTerm.Text.ToString() & _
[/SIZE][SIZE=2][COLOR=#800000]"' not found"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"No Matches"[/COLOR][/SIZE][SIZE=2], _
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
[/SIZE][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]Developer.RichTextBox1.Select(StartPosition - 1, txtSearchTerm.Text.Length)
Developer.RichTextBox1.ScrollToCaret()
Developer.Focus()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

Can anyone see anything wrong with them which would cause it not to select the text?
 
Focus before you select.

Use one of theRichTextBox.Find methods or theString.IndexOf, not the VB6 InStr function.

I missed an earlier point of your request (in case you meant "I want this to be where the cursor is in the RichTextBox" literally), that you wanted to search from where the mouse cursor was hovering, you can do that with theRichTextBox.GetCharIndexFromPosition method.
 
Back
Top