-
Oct 8th, 2016, 10:18 AM
#1
Thread Starter
Lively Member
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
-
Oct 8th, 2016, 02:50 PM
#2
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
-
Oct 9th, 2016, 08:58 PM
#3
Re: Find Multiple Text In RtxtBox and Highlight
Originally Posted by 4x2y
The CORE.txt must contains a word to search in each line like this
This would still work:
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 11th, 2016, 11:51 AM
#4
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|