NEWS

[C#] Hướng dẫn tìm kiếm và set highlight color cho RichTextBox

[C#] Hướng dẫn tìm kiếm và set highlight color cho RichTextBox
Đăng bởi: Thảo Meo - Lượt xem: 8747 09:41:37, 31/05/2018C#   In bài viết

Chào các bạn, bài viết hôm nay, mình sẽ hướng dẫn các bạn cách tìm kiếm chuỗi trên RichTextBox C#, sau khi tìm kiếm được chúng ta sẽ set Background color highlight cho chuỗi chúng ta vừa tìm kiếm được.

[C#] Search And Highlight RichTextBox Winform

Dưới đây là giao diện demo ứng dụng tìm kiếm và Highlight RichTextBox

Tutorial find in RichTextBox C#

Dưới đây, mình sẽ viết một class Extension Method cho RichTextBox.

Nếu các bạn nào chưa biết Extension Method là gì? các bạn có thể đọc bài viết của mình bên dưới

https://laptrinhvb.net/bai-viet/ebook-it/---Csharp----Gioi-thieu-va-su-dung-Extension-Method-trong-lap-trinh-csharp/39c8f31981cb13c1.html

Các bạn có thể sử dụng các lại function này cho các RichTextBox tiếp theo.

static public class Utility
    {
        public static void HighlightText(this RichTextBox myRtb, string word, Color ForeColor, Color BackgroundColor)
        {

            if (word == string.Empty)
                return;

            int s_start = myRtb.SelectionStart, startIndex = 0, index;

            while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
            {
                myRtb.Select(index, word.Length);
                myRtb.SelectionColor = ForeColor;
                myRtb.SelectionBackColor = BackgroundColor;
                startIndex = index + word.Length;
            }

            myRtb.SelectionStart = s_start;
            myRtb.SelectionLength = 0;
            myRtb.SelectionColor = ForeColor;
        }
    }

Sử dụng Full source code cho ứng dụng như sau:

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

namespace Search_hightlight_RTB
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string t;
        private void Form1_Load(object sender, EventArgs e)
        {
            myRtb.Text = File.ReadAllText("test.txt");
            t = myRtb.Text;
        }



        private void btn_find_Click(object sender, EventArgs e)
        {

            myRtb.HighlightText(tb_input.Text, Color.Pink, Color.Yellow);
        }

        private void tb_input_TextChanged(object sender, EventArgs e)
        {
            myRtb.Text = t;
            myRtb.HighlightText(tb_input.Text, Color.Pink, Color.Yellow);
        }
    }
    static public class Utility
    {
        public static void HighlightText(this RichTextBox myRtb, string word, Color ForeColor, Color BackgroundColor)
        {

            if (word == string.Empty)
                return;

            int s_start = myRtb.SelectionStart, startIndex = 0, index;

            while ((index = myRtb.Text.IndexOf(word, startIndex)) != -1)
            {
                myRtb.Select(index, word.Length);
                myRtb.SelectionColor = ForeColor;
                myRtb.SelectionBackColor = BackgroundColor;
                startIndex = index + word.Length;
            }

            myRtb.SelectionStart = s_start;
            myRtb.SelectionLength = 0;
            myRtb.SelectionColor = ForeColor;
        }
    }
}

HAVE FUN :)

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn tìm kiếm và set highlight color cho RichTextBox
Đăng bởi: Thảo Meo - Lượt xem: 8747 09:41:37, 31/05/2018C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.