?
Solved

Search RichTextBox (and regular textbox) with multiple strings...

Posted on 2003-03-16
5
Medium Priority
?
157 Views
Last Modified: 2008-03-17
Hi Everyone!

I am looking to search a RichTextBox for multiple strings.
(could be a TextBox, too, but this part of the project entails using a RichTextBox)
----------------------
Example:
Text1.Text = "First Text"
Text2.Text = "Second Text"
Text3.Text = "Third Text"
cboReplace.AddItem = "This Text"
cboReplace.AddItem = "That Text"
cboReplace.AddItem = "All Text"

rtfText.Text = "This First Text is Right before the Second.  The Second Text is right after the First. The Third Text, of course, will be last, however, if you loop through the RichTextBox Control, You'll be starting back at the First again.  Then, You'll go to the Second Text."
-----------------------

Now, what I'm trying to do is find all strings that match the entire contents of the RichTextBox above.  Hence, "First Text" Not "First" Not "Text".

For each instance of Text1.Text I find, I will assign -1 points.
For each instance of Text2.Text I find, I will assign -5 points.
For each instance of Text3.Text I find, I will assign -3 points
The Totals according to the criteria above found in the search are:
Text1.Text = -1 pts.
Text2.Text = -10 pts.
Text3.Text = -3 pts.
making the Total -14 pts.

After I find the specific text, I would like to prompt the user,
"The word '" & Text1.Text & "' has been found (Number of Times)." & vbcrlf & _
"Would You like to Replace the Text?"
If User Chooses Yes, they will choose 3 options from the options added to a Combobox named cboReplace to replace the text with (hence, user makes a choice to either not replace the text, or replace the text with a choice in the combo box.

If they choose not to replace the text, the points still remain, if they choose to change the text, the text in the rtfText.Text changes accordingly and the points will not be counted in the Total.

0
Comment
Question by:FirstBorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:FirstBorn
ID: 8148878
Thanks in Advance!

:)

(accidently submitted before closing... )
0
 
LVL 11

Accepted Solution

by:
supunr earned 860 total points
ID: 8149018
Private Sub cmdFind_Click()
    Dim txt1Count As Long
    Dim txt2Count As Long
    Dim txt3Count As Long
    Dim findChar As Long
    Dim totalPoints As Long
   
    With rtfText
        findChar = 0
        txt1Count = 0
        Do
            findChar = .Find(Text1.Text, findChar)
            If (findChar < 0) Then Exit Do
            txt1Count = txt1Count + 1
            findChar = findChar + 1
        Loop
        findChar = 0
        txt2Count = 0
        Do
            findChar = .Find(Text2.Text, findChar)
            If (findChar < 0) Then Exit Do
            txt2Count = txt2Count + 1
            findChar = findChar + 1
        Loop
        findChar = 0
        txt3Count = 0
        Do
            findChar = .Find(Text3.Text, findChar)
            If (findChar < 0) Then Exit Do
            txt3Count = txt3Count + 1
            findChar = findChar + 1
        Loop
        totalPoints = -txt1Count - txt2Count * 5 - txt3Count * 3
        If (totalPoints < 0) Then
            If (MsgBox("The word '" & Text1.Text & "' has been found " & txt1Count & " times." & vbCrLf & _
                    "Would You like to Replace the Text?", vbYesNo + vbDefaultButton2 + vbQuestion, "Replace?") = vbYes) Then
                Do
                    findChar = 0
                    findChar = .Find(Text1.Text, findChar)
                    If (findChar < 0) Then Exit Do
                    .SelText = "hello" 'cboReplace.Text ' replace text
                    findChar = findChar + 1
                Loop
            End If
        End If
    End With
End Sub

Good Luck!
0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8149392
WOW!!!  That was FAST!

Thank You VERY Much, supunr!

I am giving you an Excellent Rating and accepting this Answer First Time Around...

Could I ask you one small thing?

Is there a way you can post this code with short comments so I can follow this code to make my modifications as needed?  I am using this code with a database, and the three text boxes are actually the fields in the database that I need to use.  It's not necessary that You do (post the comments,) however, it would really help me to understand what's going on...

Thanks again!

:)
0
 
LVL 11

Expert Comment

by:supunr
ID: 8149457
thanks for the points.  here is the code again with some comments, I hope you can follow this.

Private Sub cmdFind_Click()
   Dim txt1Count As Long
   Dim txt2Count As Long
   Dim txt3Count As Long
   Dim findChar As Long
   Dim totalPoints As Long
   
   With rtfText
       findChar = 0 ' start the search from the begining of the text box
       txt1Count = 0
       Do
           findChar = .Find(Text1.Text, findChar)
           If (findChar < 0) Then Exit Do ' find return -1 if no items found, if so exit this loop
           txt1Count = txt1Count + 1 ' increament number of items found
           findChar = findChar + 1 ' start again from the next character
       Loop
       findChar = 0
       txt2Count = 0
       Do
           findChar = .Find(Text2.Text, findChar)
           If (findChar < 0) Then Exit Do
           txt2Count = txt2Count + 1
           findChar = findChar + 1
       Loop
       findChar = 0
       txt3Count = 0
       Do
           findChar = .Find(Text3.Text, findChar)
           If (findChar < 0) Then Exit Do
           txt3Count = txt3Count + 1
           findChar = findChar + 1
       Loop
       totalPoints = -txt1Count - txt2Count * 5 - txt3Count * 3 ' calculate total points count
       If (totalPoints < 0) Then
            ' confirm if the user want to replace the text or not
            ' by default select the "No" button
           If (MsgBox("The word '" & Text1.Text & "' has been found " & txt1Count & " times." & vbCrLf & _
                   "Would You like to Replace the Text?", vbYesNo + vbDefaultButton2 + vbQuestion, "Replace?") = vbYes) Then
               Do
                   findChar = 0 ' start the seach from the begining again, remember that the last occurance is already replaced.
                   findChar = .Find(Text1.Text, findChar) ' find the text
                   If (findChar < 0) Then Exit Do ' if no text is found end of seach, exit this loop
                   .SelText = "hello" 'cboReplace.Text ' replace text with whatever you want
               Loop
           End If
       End If
   End With
End Sub


0
 
LVL 1

Author Comment

by:FirstBorn
ID: 8149481
That's Awesome!

Thank You Again, Very Much, supunr!

:)
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

765 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