- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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 :)