Searching a RTB Control Question / Performance

Posted on 2003-11-20
Last Modified: 2010-05-01
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.


Question by:flosoft
  • 3

Author Comment

ID: 9792405
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.
LVL 85

Accepted Solution

Mike Tomlinson earned 250 total points
ID: 9792823
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.



Author Comment

ID: 9792985
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.

Author Comment

ID: 9795387
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.


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question