My frmFindReplace obscuring rich text box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom_ZC

    My frmFindReplace obscuring rich text box

    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

  • Mike Williams

    #2
    Re: My frmFindReplace obscuring rich text box

    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 "SendMessag eA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
    lParam As Any) As Long
    Private Const EM_GETFIRSTVISI BLELINE = &HCE
    Private Const EM_LINEFROMCHAR = &HC9

    Private Sub RichTextBox1_Ke yUp(KeyCode As Integer, _
    Shift As Integer)
    Dim j As Long, k As Long
    j = SendMessage(Ric hTextBox1.hwnd, _
    EM_GETFIRSTVISI BLELINE, 0, 0)
    k = SendMessage(Ric hTextBox1.hwnd, _
    EM_LINEFROMCHAR , RichTextBox1.Se lStart, 0)
    Caption = Format(j) + " " & Format(k)
    End Sub

    Mike



    "Tom_ZC" <noarealaddress @dontlikejunkma il.com> wrote in message
    news:s536hvkbt3 6tnsciopph6p7gt [email protected] om...[color=blue]
    > 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
    >[/color]

    Comment

    • mayayana

      #3
      Re: My frmFindReplace obscuring rich text box

      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 <noarealaddress @dontlikejunkma il.com> wrote in message
      news:s536hvkbt3 6tnsciopph6p7gt [email protected] om...[color=blue]
      > 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
      >[/color]


      Comment

      • Rick Rothstein

        #4
        Re: My frmFindReplace obscuring rich text box

        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" <noarealaddress @dontlikejunkma il.com> wrote in message
        news:s536hvkbt3 6tnsciopph6p7gt [email protected] om...[color=blue]
        > 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
        >[/color]


        Comment

        • Tom_ZC

          #5
          Re: My frmFindReplace obscuring rich text box

          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
          <noarealaddress @dontlikejunkma il.com> wrote:
          [color=blue]
          >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[/color]
          [snip]

          Comment

          Working...