Solved

Searching a RTB Control Question / Performance

Posted on 2003-11-20
4
275 Views
Last Modified: 2010-05-01
Hi,
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
DoEvents
Else
'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
rtfSource.SetFocus
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.

Thanks

0
Comment
Question by:flosoft
  • 3
4 Comments
 

Author Comment

by:flosoft
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.
0
 
LVL 85

Accepted Solution

by:
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
        Do
            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
            DoEvents
        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.

Regards,

Idle_Mind
0
 

Author Comment

by:flosoft
ID: 9792985
Hi,
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.
Flo
0
 

Author Comment

by:flosoft
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.

flo
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

705 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