-
textbox functions
I'm using the code below to find text within a text box then higlite the text..now the problem with this is, it will only fidn the first instance of the word then it will quit..im tryign to modify this code so i can add a function that will allow it to "find next" yet i have tried so many ways..
im not exactly sure what the coding is that i have to put in, but i know what i need etc :)
im thinking i need a way to starty the search at the point of where teh cursor is locatred...so therefore after the first word is located, the cursor will signify the start of the next search, and so on..except there seems to eb a problem while tryign this... please give it a shot if u think you can figure it out..
thanks alot
-joe
(put the code in a module) use the eg like this
EXAMPLE--searchHighlight(text1,"search")
Public Function searchHighlight(srchTextBox As textBox, srchString As String)
On Error resume Next
Dim A As Integer
Call srchTextBox.SetFocus
SendKeys ("^{HOME}")
A = 1
Do Until A = Len(srchTextBox.text)
'if word was found...
If Mid(UCase$(srchTextBox.text), A, Len(srchString)) = UCase$(srchString) Then
'highlight the word
For A = 1 To Len(srchString)
SendKeys ("+{RIGHT}")
Next A
Exit Do
End If
'if word isnt found or a return is found, dont do anything
If Mid(srchTextBox.text, A, 1) = Chr$(13) Then
Else
'go to next line
SendKeys ("{RIGHT}")
End If
A = A + 1
If A > Len(srchTextBox.text) Then Exit Do
Loop
End Function
?
-
Re: textbox functions
Obviously!!.., it will always find the first instance because, you have code like
...
SendKeys ("^{HOME}")
A = 1
..and ..
A = A + 1
If A > Len(srchTextBox.text) Then Exit Do
You are always searching from 1st position!!.
You could change this function to return the position (A) at which the search string was found, and let it start not with "A=1", but with some input value. and each time move that many places before searching.
I have modified your fn. It does automatic restart/loopback. It has only one gotcha: AT the end, you need to click one more.
Cut the code and paste it into a form with one text box (Text1) and a btn (command1). On each click of the btn it will search and highlight the word "hello". Try it, and correct the gotcha. You probably have to change the looping style or something...
option Explicit
private m_prevpos as Long
private Sub Command1_Click()
searchHighlight Text1, "hello", m_prevpos
End Sub
private Sub Form_Load()
FillRandumText
m_prevpos = -1
End Sub
Sub FillRandumText()
Dim ttt as string, i, j
for i = 0 to 10
ttt = vbNullString
for j = 1 to 5
ttt = ttt & Chr$((Rnd() * (Asc("z") - Asc("a"))) + Asc("a"))
next j
Text1.Text = Text1.Text & ttt & " hello "
next i
End Sub
public Function searchHighlight(srchTextBox as TextBox, srchString as string, pos as Long)
on error resume next
Dim A as Integer, i as Long, strfound as Boolean
Call srchTextBox.SetFocus
SendKeys ("^{HOME}")
If pos <> -1 then
for i = 1 to pos - 1 ' because we are moving Right.
SendKeys ("{RIGHT}")
next i
A = pos ' start from the specified pos.
else
A = 1 ' else start from the beginning
End If
Do Until A = len(srchTextBox.Text)
'if word was found...
If mid(UCase$(srchTextBox.Text), A, len(srchString)) = UCase$(srchString) then
'highlight the word
for i = 0 to len(srchString) - 1
SendKeys ("+{RIGHT}")
next i
pos = A + i
strfound = true
Exit Do
End If
'if word isnt found or a return is found, dont do anything
If mid(srchTextBox.Text, A, 1) = Chr$(13) then
else
'go to next line
SendKeys ("{RIGHT}")
End If
A = A + 1
If A > len(srchTextBox.Text) then Exit Do
Loop
If strfound = false then pos = -1
End Function
Instead of 'sendkeys' etc you could use SelStart SelLength. If the text is long and the machine slow, you can see the cursor running everytime around!! (another gotcha!)
Ravi