Ultrawhack
Well-known member
In a richtextbox, how can I highlight the entire line where a given word is found. eg "fox"
Thanks for any help.
Thanks for any help.
Dim index As Integer = Me.RichTextBox1.Find("fox")
If index <> -1 Then
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.Yellow
End If
Dim index As Integer = 0
While index <> -1
index = Me.RichTextBox1.Find("fox", index, RichTextBoxFinds.None)
If index <> -1 Then
'all matches gets here
index += 1
End If
End While
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim index As Integer = Me.RichTextBox1.Find("fox ")
If index <> -1 Then
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.Yellow
End If
End Sub
Yes, it does, but remember that you made a selection (RichTextBox1.Select). What happens if you select text in any editor and write some? Yes, you overwrite the selection. How can you unselect? You can't, but you can make a new selection. Where should this selection be? Where were you before you made the first selection? Yes, you were at SelectionStart. Pseudo:BUT... it does not allow further text entry after that
Dim WhereWasI As Integer = Me.RichTextBox1.SelectionStart
find text
If found
manipulate
Me.RichTextBox1.Select(WhereWasI, 0)
End if
Public Class Form1
Dim searchword As String = "fox"
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim SelStart As Integer = RichTextBox1.SelectionStart
Dim count As Integer = 0
Dim srchMode As RichTextBoxFinds
srchMode = RichTextBoxFinds.WholeWord
Dim wordAt As Integer = 0
Do While wordAt <> -1
wordAt = RichTextBox1.Find(searchword, wordAt, srchMode)
If wordAt = -1 Then
Exit Do
Else
HightlightLine(wordAt)
'RichTextBox1.Select(wordAt, searchword.Length)
'RichTextBox1.SelectionBackColor = Color.Yellow
wordAt += 1
count += 1
End If
Loop
RichTextBox1.SelectionStart = SelStart
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionBackColor = Color.White
End Sub
Private Sub HightlightLine(ByVal wordat As Integer)
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(wordat)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.Yellow
End Sub
End Class
Defining and filling an array/collection/dictionary of strings is pretty elementary.Is there any way on RichTextBox1_TextChanged to have a number or array of searchwords
Defining and filling an array/collection/dictionary of strings is pretty elementary.
If e.KeyValue = Keys.Enter Then
'highlight the line
Else
RichTextBox1.SelectionBackColor = Color.White
eND IF
Public Class Form1
Dim searchword() As String = {"fox", "dog", "cat"}
Dim ItemBkColor() As Color = {Color.Yellow, Color.LightSteelBlue, Color.LightSeaGreen}
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim SelStart As Integer = RichTextBox1.SelectionStart
Dim count As Integer = 0
Dim srchMode As RichTextBoxFinds
srchMode = RichTextBoxFinds.WholeWord
For SearchWordPointer As Integer = 0 To searchword.Length - 1
Dim wordAt As Integer = 0
Do While wordAt <> -1
wordAt = RichTextBox1.Find(searchword(SearchWordPointer), wordAt, srchMode)
If wordAt = -1 Then
Exit Do
Else
HightlightLine(wordAt, ItemBkColor(SearchWordPointer))
wordAt += 1
count += 1
End If
Loop
Next
RichTextBox1.SelectionStart = SelStart
RichTextBox1.SelectionLength = 0
RichTextBox1.SelectionBackColor = Color.White
End Sub
Private Sub HightlightLine(ByVal wordat As Integer, ByVal BG_Color As Color)
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(wordat)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = BG_Color
End Sub