- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
[C#] Hướng dẫn tìm kiếm và set highlight color cho RichTextBox
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
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
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 :)