- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[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 :)