Searching for a string in a richtextbox control

bjwade62
09-11-2003, 07:50 AM
The answer is probably posted here someplace but I can't find it. Sounds simple enough.

Here's what I want to do.

1. Enter text in a textbox control
2. Click a button
3. Search a richtextbox control for the text1.text string
4. Highlight matching string in richtextbox control

Any help would be appreciated,
Bernie

nsmoller
09-11-2003, 08:02 AM
very easy indeed

i believe its richtext.upto and string, check msdn for more parameters


mystring=text1.text
richtext.upto(mystring) 'moves insertion point (flashing text thing) UPTO search string
richtext.span mystring 'selects the string


if you want to do something with it - richtext.selRTF not seltext ok??

Flyguy
09-11-2003, 08:05 AM
Actually it's called the .Find method.
Example from MSDN:

Private Sub Command1_Click()
Dim FoundPos As Integer
Dim FoundLine As Integer
' Find the text specified in the TextBox control.
FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)

' Show message based on whether the text was found or not.

If FoundPos <> -1 Then
' Returns number of line containing found text.
FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
MsgBox "Word found on line " & CStr(FoundLine)
Else
MsgBox "Word not found."
End If
End Sub

nsmoller
09-11-2003, 08:09 AM
i think the upto/span though is more appropriate for him

bjwade62
09-11-2003, 08:15 AM
Thanks. That worked but how do I get it to highlight the found text instead of reporting the line number?

Actually it's called the .Find method.
Example from MSDN:

Private Sub Command1_Click()
Dim FoundPos As Integer
Dim FoundLine As Integer
' Find the text specified in the TextBox control.
FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)

' Show message based on whether the text was found or not.

If FoundPos <> -1 Then
' Returns number of line containing found text.
FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
MsgBox "Word found on line " & CStr(FoundLine)
Else
MsgBox "Word not found."
End If
End Sub

SpaceFrog
09-11-2003, 08:16 AM
What about instr, selstart and sellength, does that sound prehistorical ?

nsmoller
09-11-2003, 08:16 AM
try the upto and span... i think u get my point right?

selstart=foundpos
sellength=len(text1.text)
if you use flyguy's method

SpaceFrog
09-11-2003, 08:19 AM
Thanks! Just learning a lot ...

bjwade62
09-15-2003, 08:05 AM
Thanks Flyguy,
That works great to find the first instance of a string, but what if I want to continue searching for every instance of a string?

Thanks,
Bernie


Actually it's called the .Find method.
Example from MSDN:

Private Sub Command1_Click()
Dim FoundPos As Integer
Dim FoundLine As Integer
' Find the text specified in the TextBox control.
FoundPos = RichTextBox1.Find(Text1.Text, , , rtfWholeWord)

' Show message based on whether the text was found or not.

If FoundPos <> -1 Then
' Returns number of line containing found text.
FoundLine = RichTextBox1.GetLineFromChar(FoundPos)
MsgBox "Word found on line " & CStr(FoundLine)
Else
MsgBox "Word not found."
End If
End Sub

Flyguy
09-15-2003, 08:20 AM
Did you take the time to look at the other parameters used for the Find method??

bjwade62
09-15-2003, 09:29 AM
Actually, yes I did. Ialso just looked again to see if I overlooked something. Maybe it's there, but if it is I'm not seeing it.

Did you take the time to look at the other parameters used for the Find method??

Thinker
09-15-2003, 10:07 AM
The vStart and vEnd parameters let you limit your search to a part of
the text. By using vStart set to begin after the last found occurrence,
you can continue the search.

bjwade62
09-15-2003, 11:47 AM
Wow. I must be dense these days. I'm spending way too much time on this without solving it. I've tried using the vStart parameter as you said but it's still not working. By the way I'm not re-dim(ing) the FoundPos variable so it not being reset. I'm at a loss.


The vStart and vEnd parameters let you limit your search to a part of
the text. By using vStart set to begin after the last found occurrence,
you can continue the search.

Thinker
09-15-2003, 11:49 AM
Are you incrementing the FoundPos variable like I also recommended?

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum