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 "lngFoundPos" 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_Click()
Dim intRetVal As Integer
Dim lngFoundPos As Long
Dim blnWholeWords As Boolean
Dim strFrontWholeCheck As String, strBackWholeCheck 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.Value = vbChecked Then
blnWholeWords = True
Else
blnWholeWords = False
End If
strSearchString = txtFind.Text
'change mouse to hourglass
Screen.MousePointer = 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((lngLastPos + 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
strFrontWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos -
1), 1)
intWordLen = Len(strSearchString)
strBackWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos +
intWordLen), 1)
If strFrontWholeCheck = " " And strBackWholeCheck = " " 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.MousePointer = 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.MousePointer = vbDefault
intRetVal = MsgBox("No more occurrences of word were found.",
vbInformation, "Not Found")
If intRetVal = vbOK Then
Unload Me
End If
Else
Screen.MousePointer = vbDefault
With frmMain
.rtfPage.SelStart = lngFoundPos - 1
.rtfPage.SelLength = Len(strSearchString)
End With
frmMain.SetFocus
End If
lngLastPos = lngFoundPos
intSearchNum = intSearchNum + 1
intFindNum = intFindNum + 1
End Sub 4 4508
You can send all sorts of messages to the RTB to get all kinds of
information from it. The following simple routine gets the number of the
line that contains the current SelStart position and also the number of the
topmost visible line in the RTB. Between them these values will give you the
information you want. You can also get horizontal positions and all sorts of
other stuff. Try this simple example:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Const EM_LINEFROMCHAR = &HC9
Private Sub RichTextBox1_KeyUp(KeyCode As Integer, _
Shift As Integer)
Dim j As Long, k As Long
j = SendMessage(RichTextBox1.hwnd, _
EM_GETFIRSTVISIBLELINE, 0, 0)
k = SendMessage(RichTextBox1.hwnd, _
EM_LINEFROMCHAR, RichTextBox1.SelStart, 0)
Caption = Format(j) + " " & Format(k)
End Sub
Mike
"Tom_ZC" <no************@dontlikejunkmail.com> wrote in message
news:s5********************************@4ax.com... 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 "lngFoundPos" 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_Click()
Dim intRetVal As Integer Dim lngFoundPos As Long Dim blnWholeWords As Boolean Dim strFrontWholeCheck As String, strBackWholeCheck 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.Value = vbChecked Then blnWholeWords = True Else blnWholeWords = False End If
strSearchString = txtFind.Text
'change mouse to hourglass Screen.MousePointer = 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((lngLastPos + 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 strFrontWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos - 1), 1) intWordLen = Len(strSearchString) strBackWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos + intWordLen), 1) If strFrontWholeCheck = " " And strBackWholeCheck = " " 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.MousePointer = 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.MousePointer = vbDefault intRetVal = MsgBox("No more occurrences of word were found.", vbInformation, "Not Found") If intRetVal = vbOK Then Unload Me End If Else Screen.MousePointer = vbDefault With frmMain .rtfPage.SelStart = lngFoundPos - 1 .rtfPage.SelLength = Len(strSearchString) End With frmMain.SetFocus End If
lngLastPos = lngFoundPos intSearchNum = intSearchNum + 1 intFindNum = intFindNum + 1
End Sub
I think you can use EM_POSFROMCHAR.
It returns the coordinates of a given character. Assuming that
your Find is highlighting the text you should just need to get the offset
of SelStart, send it to EM_POSFROMCHAR, and compare that to
the Find window coordinates.
--
--
Tom_ZC <no************@dontlikejunkmail.com> wrote in message
news:s5********************************@4ax.com... 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 "lngFoundPos" 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_Click()
Dim intRetVal As Integer Dim lngFoundPos As Long Dim blnWholeWords As Boolean Dim strFrontWholeCheck As String, strBackWholeCheck 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.Value = vbChecked Then blnWholeWords = True Else blnWholeWords = False End If
strSearchString = txtFind.Text
'change mouse to hourglass Screen.MousePointer = 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((lngLastPos + 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 strFrontWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos - 1), 1) intWordLen = Len(strSearchString) strBackWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos + intWordLen), 1) If strFrontWholeCheck = " " And strBackWholeCheck = " " 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.MousePointer = 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.MousePointer = vbDefault intRetVal = MsgBox("No more occurrences of word were found.", vbInformation, "Not Found") If intRetVal = vbOK Then Unload Me End If Else Screen.MousePointer = vbDefault With frmMain .rtfPage.SelStart = lngFoundPos - 1 .rtfPage.SelLength = Len(strSearchString) End With frmMain.SetFocus End If
lngLastPos = lngFoundPos intSearchNum = intSearchNum + 1 intFindNum = intFindNum + 1
End Sub
I only looked quickly at your question, but I'm pretty sure Mike has given
you enough to work with for the form-location problem. On the other hand, a
quick look at your code seems to indicate you are doing more work than
necessary in the "find" part of your code. You say you are using a
RichTextBox... are you aware it has a built-in Find method that will
efficiently locate text and allow you to specify whether that text is a
whole word or not? Check it out in the help files; I think you will find (no
pun intended) that it will simplify your code quite a bit.
Rick - MVP
"Tom_ZC" <no************@dontlikejunkmail.com> wrote in message
news:s5********************************@4ax.com... 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 "lngFoundPos" 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_Click()
Dim intRetVal As Integer Dim lngFoundPos As Long Dim blnWholeWords As Boolean Dim strFrontWholeCheck As String, strBackWholeCheck 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.Value = vbChecked Then blnWholeWords = True Else blnWholeWords = False End If
strSearchString = txtFind.Text
'change mouse to hourglass Screen.MousePointer = 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((lngLastPos + 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 strFrontWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos - 1), 1) intWordLen = Len(strSearchString) strBackWholeCheck = Mid(frmMain.rtfPage.Text, (lngFoundPos + intWordLen), 1) If strFrontWholeCheck = " " And strBackWholeCheck = " " 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.MousePointer = 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.MousePointer = vbDefault intRetVal = MsgBox("No more occurrences of word were found.", vbInformation, "Not Found") If intRetVal = vbOK Then Unload Me End If Else Screen.MousePointer = vbDefault With frmMain .rtfPage.SelStart = lngFoundPos - 1 .rtfPage.SelLength = Len(strSearchString) End With frmMain.SetFocus End If
lngLastPos = lngFoundPos intSearchNum = intSearchNum + 1 intFindNum = intFindNum + 1
End Sub
Thank you all for your help. That's the info I need to know. And, to
Rick -- no, I did not know about those capabilities of the Rich Text
Box. Thanks for the heads up on them -- that will simiplify my coding
a great deal.
cheers,
Tom
On Mon, 14 Jul 2003 19:56:25 GMT, Tom_ZC
<no************@dontlikejunkmail.com> wrote: 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
[snip] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alfredo Agosti |
last post by:
Hi folks,
I have an Access 2000 db with a memo field.
Into the memo field I put text with bold attributes, URL etc etc
What I need to to is converting the rich text contained into the memo...
|
by: Pjotr Wedersteers |
last post by:
I am new to J(ava)Script, use PHP a lot and consider moving some stuff for a
project over to the client side. Problem is part of the PHP code is
copyrighted and the author would not be happy to see...
|
by: PC User |
last post by:
I found this Rich Text Editor and I've been trying to recreate it in
my own application. I've had trouble with the COMCTL.ImageListCtrl
and the COMCTL.Toolbar to recreate the toolbar. And I've...
|
by: Peter |
last post by:
I have written a small terminal app that reads and writes to the serial port. In particular as the data is read from the serial port it is appended to the rich text box. The problem I am...
|
by: ray well |
last post by:
hi,
my app has two parallel rich text boxes containing the same content in 2
different languages. the lines parallel each other, line #3 in english
contains the same content as line #3 in...
|
by: tomi.trescak |
last post by:
Hi
I have a problem with storing rich text in MySQL.
I store rich text in MySQL (in column with type "text") which i get
from Rich Textbox control. When i do reverse processing by trying to...
|
by: Neil |
last post by:
I need to implement a rich text box in an MDB file for a user base that
consists of Access 2000 and Access 2002. Unfortunately, I'm using Access
2003 on my development machine. My understanding is...
|
by: Neil |
last post by:
Just found out that the Microsoft Rich Textbox does not support full text
justification, since it's based on Version 1.0 of the RichEdit Window Class,
and full text justification is only available...
|
by: Neil |
last post by:
I posted a few days ago that it seems to me that the Access 2007 rich text
feature does not support: a) full text justification; b) programmatic
manipulation.
I was hoping that someone might...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |