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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now