I'm writing a find and replace routine for a text editor I'm working
on. The editor has a rich text box on the main form (frmMain). The
find/replace form is modeled after the one in Word '97/2K. So far my
find code is working satisfactorily. (The replace code is not yet
written.)
One challenge has me stumped thus far. I need to find a way to make
sure my frmFindReplace does not obscure the view of the found word
that has been hilighted in the rich text box. You may notice in Word
if the found word would have been behind the find/replace form, the
form jumps out of the way so you can see it.
My find code uses a long variable named "lngFoundPo s" to determine
where the word is that the user was looking for. I'm wondering if
there's some way to convert that value into a top property. If I had
such a top property, I could calculate a new position for
frmFindReplace based on its top and height properties. Piece of cake
once I get this top value I need.
Any ideas on how I could do this?
Here's my code (watch for word wrap trouble):
Private Sub cmdFindNext_Cli ck()
Dim intRetVal As Integer
Dim lngFoundPos As Long
Dim blnWholeWords As Boolean
Dim strFrontWholeCh eck As String, strBackWholeChe ck As String '
check spaces in front and back of word to see if whole
Dim intWordLen As Integer ' length of the word
Static lngLastPos As Long
Static intSearchNum As Integer, intFindNum As Integer
Dim intWordTop As Integer ' a word's top location so that we
can avoid having the find form cover the word
If chkWholeWords.V alue = vbChecked Then
blnWholeWords = True
Else
blnWholeWords = False
End If
strSearchString = txtFind.Text
'change mouse to hourglass
Screen.MousePoi nter = vbHourglass
FindWord:
'search for text, case insensitive
If intSearchNum = 0 Then ' first search, start from beginning
lngFoundPos = InStr(1, frmMain.rtfPage .Text, strSearchString ,
1)
Else
lngFoundPos = InStr((lngLastP os + 1), frmMain.rtfPage .Text,
strSearchString , 1)
End If
If blnWholeWords = True And lngFoundPos <> 0 Then ' they only
want whole words
' check and make sure it's a whole word, otherwise search for
another
strFrontWholeCh eck = Mid(frmMain.rtf Page.Text, (lngFoundPos -
1), 1)
intWordLen = Len(strSearchSt ring)
strBackWholeChe ck = Mid(frmMain.rtf Page.Text, (lngFoundPos +
intWordLen), 1)
If strFrontWholeCh eck = " " And strBackWholeChe ck = " " Then
'It's a whole word
Else
'It's not a whole word, try again
lngLastPos = lngFoundPos
intSearchNum = intSearchNum + 1
GoTo FindWord ' I too am a sinner
End If
End If
If lngFoundPos = 0 And intSearchNum = 0 Then
Screen.MousePoi nter = vbDefault
intRetVal = MsgBox("Text was not found.", vbInformation, "Not
Found")
If intRetVal = vbOK Then
Unload Me
End If
ElseIf lngFoundPos = 0 And intFindNum > 0 Then ' we've searched
before, but didn't find this time
Screen.MousePoi nter = vbDefault
intRetVal = MsgBox("No more occurrences of word were found.",
vbInformation, "Not Found")
If intRetVal = vbOK Then
Unload Me
End If
Else
Screen.MousePoi nter = vbDefault
With frmMain
.rtfPage.SelSta rt = lngFoundPos - 1
.rtfPage.SelLen gth = Len(strSearchSt ring)
End With
frmMain.SetFocu s
End If
lngLastPos = lngFoundPos
intSearchNum = intSearchNum + 1
intFindNum = intFindNum + 1
End Sub
on. The editor has a rich text box on the main form (frmMain). The
find/replace form is modeled after the one in Word '97/2K. So far my
find code is working satisfactorily. (The replace code is not yet
written.)
One challenge has me stumped thus far. I need to find a way to make
sure my frmFindReplace does not obscure the view of the found word
that has been hilighted in the rich text box. You may notice in Word
if the found word would have been behind the find/replace form, the
form jumps out of the way so you can see it.
My find code uses a long variable named "lngFoundPo s" to determine
where the word is that the user was looking for. I'm wondering if
there's some way to convert that value into a top property. If I had
such a top property, I could calculate a new position for
frmFindReplace based on its top and height properties. Piece of cake
once I get this top value I need.
Any ideas on how I could do this?
Here's my code (watch for word wrap trouble):
Private Sub cmdFindNext_Cli ck()
Dim intRetVal As Integer
Dim lngFoundPos As Long
Dim blnWholeWords As Boolean
Dim strFrontWholeCh eck As String, strBackWholeChe ck As String '
check spaces in front and back of word to see if whole
Dim intWordLen As Integer ' length of the word
Static lngLastPos As Long
Static intSearchNum As Integer, intFindNum As Integer
Dim intWordTop As Integer ' a word's top location so that we
can avoid having the find form cover the word
If chkWholeWords.V alue = vbChecked Then
blnWholeWords = True
Else
blnWholeWords = False
End If
strSearchString = txtFind.Text
'change mouse to hourglass
Screen.MousePoi nter = vbHourglass
FindWord:
'search for text, case insensitive
If intSearchNum = 0 Then ' first search, start from beginning
lngFoundPos = InStr(1, frmMain.rtfPage .Text, strSearchString ,
1)
Else
lngFoundPos = InStr((lngLastP os + 1), frmMain.rtfPage .Text,
strSearchString , 1)
End If
If blnWholeWords = True And lngFoundPos <> 0 Then ' they only
want whole words
' check and make sure it's a whole word, otherwise search for
another
strFrontWholeCh eck = Mid(frmMain.rtf Page.Text, (lngFoundPos -
1), 1)
intWordLen = Len(strSearchSt ring)
strBackWholeChe ck = Mid(frmMain.rtf Page.Text, (lngFoundPos +
intWordLen), 1)
If strFrontWholeCh eck = " " And strBackWholeChe ck = " " Then
'It's a whole word
Else
'It's not a whole word, try again
lngLastPos = lngFoundPos
intSearchNum = intSearchNum + 1
GoTo FindWord ' I too am a sinner
End If
End If
If lngFoundPos = 0 And intSearchNum = 0 Then
Screen.MousePoi nter = vbDefault
intRetVal = MsgBox("Text was not found.", vbInformation, "Not
Found")
If intRetVal = vbOK Then
Unload Me
End If
ElseIf lngFoundPos = 0 And intFindNum > 0 Then ' we've searched
before, but didn't find this time
Screen.MousePoi nter = vbDefault
intRetVal = MsgBox("No more occurrences of word were found.",
vbInformation, "Not Found")
If intRetVal = vbOK Then
Unload Me
End If
Else
Screen.MousePoi nter = vbDefault
With frmMain
.rtfPage.SelSta rt = lngFoundPos - 1
.rtfPage.SelLen gth = Len(strSearchSt ring)
End With
frmMain.SetFocu s
End If
lngLastPos = lngFoundPos
intSearchNum = intSearchNum + 1
intFindNum = intFindNum + 1
End Sub
Comment