Results 1 to 4 of 4

Thread: Find Multiple Text In RtxtBox and Highlight

  1. #1

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Question Find Multiple Text In RtxtBox and Highlight

    What I have is two Richtextboxes. One will have multiple lines of text that will be searched for in the other Richtextbox.
    So let's say Richtextbox2.text has this:
    Hello
    My
    Name
    Is

    All of those lines need to stand as a individual string that will be searched and highlighted in blue in Richtextbox1.
    Hello friends and family, my last name is Haas and my first is Kory.

    So every instance of Hello, My, Name, Is should be highlighted blue.
    Hello friends and family, my last name is Haas and my first is Kory.

    The code I am using to do this is one I tried to build myself and isn't work well, sometimes it will highlight the first couple instances and sometimes it'll only highlight one instance. I can't figure out what is wrong with my code but I'm thinking about starting completely from scratch. But I am completely open and thankful for all ideas and help.

    Code:
     If System.IO.File.Exists(Application.StartupPath & "\DUMP\CORE.txt") Then
                Dim objreader As New System.IO.StreamReader(Application.StartupPath & "\DUMP\CORE.txt")
                For index As Integer = 1 To CORE_Line_Count
                    Dim Find_X As String = objreader.ReadLine
                    MsgBox(CORE_Line_Count)
                    Dim lastindex = RichTextBox1.Text.LastIndexOf(Find_X)
                    index = 0
                    While index < lastindex
                        RichTextBox1.Find(Find_X, index, RichTextBoxFinds.None)
                        RichTextBox1.SelectionColor = Color.Blue
                        index = RichTextBox1.Text.IndexOf(Find_X, index) + 1
                    End While
                    'Textbox2.text = objreader.ReadToEnd
                Next
                objreader.Close()
            End If

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Find Multiple Text In RtxtBox and Highlight

    Try this
    Code:
    Option Explicit On
    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            RichTextBox1.Text = "Hello friends and family, my last name is Haas and my first is Kory."
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim strWordToFind() As String = IO.File.ReadAllLines(Application.StartupPath & "\DUMP\CORE.txt")
            Dim intIndex As Integer
    
            For j = 0 To strWordToFind.Length - 1
                intIndex = RichTextBox1.Find(strWordToFind(j), 0, RichTextBoxFinds.None)
                Do While intIndex > -1
                    RichTextBox1.Select(intIndex, strWordToFind(j).Length)
                    RichTextBox1.SelectionColor = Color.Blue
                    intIndex = RichTextBox1.Find(strWordToFind(j), intIndex + strWordToFind(j).Length, RichTextBoxFinds.None)
                Loop
            Next
    
        End Sub
    
    End Class
    The CORE.txt must contains a word to search in each line like this
    Code:
    Hello
    My
    Name
    Is



  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Find Multiple Text In RtxtBox and Highlight

    Quote Originally Posted by 4x2y View Post
    The CORE.txt must contains a word to search in each line like this
    Code:
    Hello
    My
    Name
    Is
    This would still work:

    Hello


    My

    Name
    Is


  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Find Multiple Text In RtxtBox and Highlight

    I have a RichTextBox (rtbSec) that I needed to highlight certain strings. In my case the case was ignored. When there were a lot of matches I found the .Find method to be slow, so I came up with this which is much faster.

    Code:
        Private Function rtbSecHighLight(WordToHighLight As String, HighLightColor As Color) As Integer
            'this was originally written using
            '  rtbSec.Find() which was SLOW!!
            Dim rv As Integer = 0
            Dim rtbLower As String = rtbSec.Text.ToLower
            Dim rl As Integer = rtbLower.Length
    
            Dim wd As String = WordToHighLight.ToLower
            Dim wl As Integer = WordToHighLight.Length
    
            Dim idx As Integer
    
            idx = rtbLower.IndexOf(wd, 0)
            Do While idx >= 0
                rv += 1
                rtbSec.Select(idx, wl)
                rtbSec.SelectionBackColor = fbgc
                rtbSec.SelectionColor = HighLightColor
                idx += wl
                If idx >= rl Then Exit Do
                idx = rtbLower.IndexOf(wd, idx)
            Loop
    
            Return rv
    
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width