Searching a RTB Control Question / Performance

I need to search for a particular list of words in a rich text control. I can do this no problem, but what I want is to show some visual feedback to my users, I have accomplished this partially with the code below.

Private Sub CheckBadWords()
On Error Resume Next
mCancel = False
Dim z As Long
Dim a() As String
Dim lngResult2 As Long
Dim i As Integer
Dim lngresult As Long
Dim lngPos As Long
Dim NumWords As Integer
pbar.Value = 0
pBar2.Value = 0
pBar2.Visible = True
Label4.Visible = True
Label3.Visible = True
btnRecheck.Visible = True
btnRecheck.Caption = "Cancel"
pbar.Visible = True
pbar.Max = frmAdmin.lstBadWords.ListCount - 1
rtfSource.MousePointer = vbHourglass
frmAdmin.lstBadWords.ListIndex = 0

If Inet1.StillExecuting = True Then
'found = 0
oSourceStr = Inet1.OpenURL(frmAdmin.QuickBrowse.LocationURL)
rtfSource.Text = oSourceStr
a() = Split(oSourceStr, " ")
NumWords = UBound(a)
pBar2.Max = NumWords
For i = 0 To frmAdmin.lstBadWords.ListCount - 1
Me.Caption = "Checking for Word " & frmAdmin.lstBadWords.Text

For z = 0 To UBound(a)
        If a(z) = "" Then
        a(z) = 1
        End If
        'lngresult = rtfSource.Find(frmAdmin.lstBadWords.Text, lngPos, , 0)
        lngResult2 = rtfSource.Find(a(z), lngPos, , 0)
        lngPos = rtfSource.SelStart + rtfSource.SelLength
If mCancel = True Then
MsgBox "Check Cancelled", vbInformation, "Bad Word Check"
cmdOk.Enabled = True
pbar.Visible = False
pBar2.Visible = False
Label4.Visible = False
Label3.Visible = False
rtfSource.MousePointer = vbNormal
Exit Sub
End If
pBar2.Value = z
Pause 0
Next z
lngresult = rtfSource.Find(frmAdmin.lstBadWords.Text, 0, , 0)
frmAdmin.lstBadWords.ListIndex = i

If lngresult <> -1 Then
rtfSource.SelColor = vbBlue
rtfSource.SelBold = True
rtfSource.SelFontSize = 12
Me.Caption = "Check Complete - Bad Word Found"
MsgBox "Bad Words found on this site", vbInformation, "Bad Word Check"
rtfSource.MousePointer = vbNormal
pbar.Visible = False
pBar2.Visible = False
Label4.Visible = False
Label3.Visible = False
btnRecheck.Visible = True
btnRecheck.Caption = "ReCheck"
cmdOk.Enabled = True
Exit Sub
End If
'Pause Combo1.Text
pbar.Value = i
lngPos = 0
Next i

Me.Caption = "Check Complete"
MsgBox "This site has passed the bad words check!" & vbCrLf _
& "Words Checked: " & NumWords, vbInformation, "Bad words check"
rtfSource.MousePointer = vbNormal
pbar.Visible = False
pBar2.Visible = False
Label4.Visible = False
Label3.Visible = False
btnRecheck.Visible = True
btnRecheck.Caption = "ReCheck"
cmdOk.Enabled = True
End If
Screen.MousePointer = vbNormal
End Sub

pbar1 and pbar2 are progress bars 1 indicating total progress, the other the current word being looked for's progress. This all seems to work ok on smaller sites, however larger ones go slow, or the rtf jumps to the end and what I want is the current word to blaze by and the total to go however fast it needs to, I have tried this in reverse with the loop backwords( each word get checked by the bad words list instead) To make a long story short, I want to have each word highlighted as it goes through and the 2 progress bars to update so the user had some idea that the search is taking place. and to have all this happen in a consistent and stable manner. Also if someone can offer a way via the dom or whatever to read an entire webpages source via the browser control instead of using an inet control to pull the text, this would also be of great benifit.

As always any help is appreciated.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flosoftAuthor Commented:
Update to make it a little clearer.

I have loaded a websites source into a richtextbox control.

I am checking the contents against a bad word list.

The process should show the user:
1. Each word in the source is visited and highlighted, while the rtf scrolls.
2.Updates a progress bar for each word and total progess as it is going alond.
2a. This could be a check of Word Against source as above, or Source against word.
3. If a badword if found, it should highlight and bold the bad word.

The code posted does this (Word against source and loops for each word, but may seem confusing as I did not comment this. and it seems to be a slower way than what is probably possible.

I think this may clear up the question a little bit better.

Thanks again.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Your doing way too much work to find the bad words my friend.

You started off great by iterating through the bad words list:

     For i = 0 To frmAdmin.lstBadWords.ListCount - 1

but then you when went off on a tangent trying to do something with the words you had placed in the a() array with split.  

This small little snippet shows how to generically search an RTF box for the strings from a listbox and highlight them.  It was fast on my system, but then I don't know how many words you were checking for or how big the web page was.

    Dim a As Integer
    Dim result As Integer
    Dim startpos As Long
    startpos = 0
    For a = 0 To List1.ListCount - 1
        startpos = 0 ' Start search from beginning
            result = RichTextBox1.Find(List1.List(a), startpos, , rtfWholeWord)
            If result <> -1 Then ' If found word, hightlight and update startPos
                RichTextBox1.SelStart = result
                RichTextBox1.SelLength = Len(List1.List(a))
                RichTextBox1.SelColor = QBColor(12) ' Light Red
                RichTextBox1.SelBold = True
                startpos = result + Len(List1.List(a))
            End If
        Loop While result <> -1
    Next a

That's it!  It should run a lot faster since it doesn't have the extra loop that you did going through the words in the web page.

Of course, you could add in the ability to cancel the search and add in your progress bars.  To get your progress bars to update you have to add in the DoEvents command.  It allows windows to process the messages for your app, including the one telling the progressbar to update, before continuing on with the next line of code.  Without it, your program gets stuck in the For loops and doesn't allow the program to process messages in its queue.

Hope this helps illustrate how to find and highlight the bad words a little clearer for you.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flosoftAuthor Commented:
I already had done something just like that what I was shooting for was visiting (highlighting) every word in the rtb as the search went along. This would show a user that each word was getting visited, I know the way presented is quicker, but it provided less graphical interaction. this is why I split the source into an array, using the find method to visit each word has to part of why it is slow, there has to be a better way to highlight each word than that, but I am at a loss at the moment on that one.

If you run my code, although slower, you will see each word getting highlighted against the list, and 1 progess bar updating per word in the rtf (current progress) and the second updating per word in the list (total progress). I would like a better faster way to have a simular situation.

I do thank you for your input and look forward to any more suggestion.

I am also still trying to pull the full source out of the Browser control rather than using an inet control causing 2 downloads per url to occur, if anyone knows how to do this, that would also be greatly appreciated.

Thanks again.
flosoftAuthor Commented:
I guess it will be fine to use the way I was.

A note of interest, instead of using:
rtfWholeWord as a flag, use 0. Less bad words will get buy.

I am still needing a way to pull the full source of a web browser control instead of having to use an inet or other means to load a textbox.

I have experimented with using the MSHTML and MSDHTML .dll but can only seem to pull inner / outer text or inner / outer html, but not in the correct layout.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.