C# - Search And Highlight Text In A RichTextBox

C# - How To Search And Highlight Text In A RichTextBox Using C#


In This C# Tutorial We Will See How To Search And Highlight Text In A RichTextBox 
Using CSharp Programming Language .


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Search_Text : Form
    {
        public Search_Text()
        {
            InitializeComponent();
        }

        private void BTN_SEARCH_Click(object sender, EventArgs e)
        {

            int start = 0;
            int end = richTextBox1.Text.LastIndexOf(textBox1.Text);

            richTextBox1.SelectAll();
            richTextBox1.SelectionBackColor = Color.White;

            while(start < end)
            {
                richTextBox1.Find(textBox1.Text, start, richTextBox1.TextLength, RichTextBoxFinds.MatchCase);

                richTextBox1.SelectionBackColor = Color.Yellow;

                start = richTextBox1.Text.IndexOf(textBox1.Text, start) + 1;
            }

        }
    }
}

///////////////OUTPUT:

c# search and highlight text




Share this

Related Posts

Previous
Next Post »