Hi aginell4life,
Refer below sample.
I have added a TextBox, a Button and a RichTextBox control to the Form.
Namespaces
C#
using System.Linq;
using System.Collections.Generic;
VB.Net
Imports System.Linq
Imports System.Collections.Generic
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = "Writing about yourself can seem embarrassing at first. " +
"Cover letters, personal essays, and bio notes about yourself come with some " +
"specific tricks and tips that can make it a lot less intimidating when choosing style and content. " +
"Learn the basics and you will be able to make your personal writing stand out";
txtWord.Text = "will";
}
public void hl(RichTextBox T, string Word, Color color1)
{
T.Text = "Writing about yourself can seem embarrassing at first. " +
"Cover letters, personal essays, and bio notes about yourself come with some " +
"specific tricks and tips that can make it a lot less intimidating when choosing style and content. " +
"Learn the basics and you will be able to make your personal writing stand out";
List<string> statements = richTextBox1.Text.Split('.').ToList();
for (int j = statements.Count - 1; j >= 0; j += -1)
{
if (!statements[j].Trim().ToLower().Contains(txtWord.Text.ToLower().Trim()))
statements.RemoveAt(j);
}
T.Text = string.Join(".", statements);
int pos = 0;
string s = T.Text;
int i = 0;
bool StopWhile = false;
while (!StopWhile)
{
int j = s.IndexOf(Word, i);
if (j < 0)
StopWhile = true;
else
{
T.Select(j, Word.Length);
T.SelectionColor = color1;
T.SelectionBackColor = Color.Yellow;
i = j + 1;
}
}
T.Select(pos, 0);
}
private void btnHighlight_Click(object sender, EventArgs e)
{
hl(richTextBox1, txtWord.Text, Color.Red);
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
richTextBox1.Text = "Writing about yourself can seem embarrassing at first. " +
"Cover letters, personal essays, and bio notes about yourself come with some " +
"specific tricks and tips that can make it a lot less intimidating when choosing style and content. " +
"Learn the basics and you will be able to make your personal writing stand out"
txtWord.Text = "will"
End Sub
Public Sub hl(ByVal T As RichTextBox, ByVal Word As String, ByVal color1 As Color)
T.Text = "Writing about yourself can seem embarrassing at first. " +
"Cover letters, personal essays, and bio notes about yourself come with some " +
"specific tricks and tips that can make it a lot less intimidating when choosing style and content. " +
"Learn the basics and you will be able to make your personal writing stand out"
Dim statements As List(Of String) = richTextBox1.Text.Split("."c).ToList()
For j As Integer = statements.Count - 1 To 0 Step -1
If Not statements(j).Trim().ToLower().Contains(txtWord.Text.ToLower().Trim()) Then
statements.RemoveAt(j)
End If
Next
T.Text = String.Join(".", statements)
Dim pos As Integer = 0
Dim s As String = T.Text
Dim i As Integer = 0
Dim StopWhile As Boolean = False
While Not StopWhile
Dim j As Integer = s.IndexOf(Word, i)
If j < 0 Then
StopWhile = True
Else
T.Select(j, Word.Length)
T.SelectionColor = color1
T.SelectionBackColor = Color.Yellow
i = j + 1
End If
End While
T.Select(pos, 0)
End Sub
Private Sub btnHighlight_Click(sender As Object, e As EventArgs) Handles btnHighlight.Click
hl(richTextBox1, txtWord.Text, Color.Red)
End Sub
Screenshot