- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
- [C#] Thiết kế ứng dụng Console đẹp với thư viện Spectre.Console
- [C#] Thiết kế ứng dụng Single Instance và đưa ứng dụng lên trước nếu kiểm tra ứng dụng đang chạy
- [C#] Giới thiệu JSON Web Token và cách đọc chuỗi token
- [C#] Cách tăng giảm font chữ tất cả các control trên winform
- [DEVEXPRESS] Tích hợp chức năng Tìm kiếm Search vào CheckedComboboxEdit
- [C#] Gởi email Metting Calendar Reminder kèm nhắc thời gian lịch họp
- [C#] Tìm kiếm xem danh sách từ khóa có tồn tại trong đoạn văn bản hay không
- [C#] Thiết kế giao diện ứng dụng trên Console sử dụng thư viện Terminal.Gui
- [C#] Hướng dẫn tạo mã VietQR Payment API Winform
- [C#] Sử dụng thư viện BenchmarkDotNet đo hiệu năng của hảm Method
- [DEVEXPRESS] Tìm kiếm không dấu tô màu highlight có dấu trên C# Winform
- [C#] Chia sẻ source code tạo hiệu ứng pixel Image trên winform
- [C#] Hướng dẫn kiểm tra số Container hợp lệ hay không
[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 :)