- [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
- [DATABASE] Hướng dẫn sử dụng STRING_AGG và CONCAT_WS trong sqlserver 2017
- [C#] Hướng dẫn Copy With All Property in Class Object
- [DEVEXPRESS] Hướng dẫn load Json DataSource vào GridView
- [C#] Hướng dẫn tạo chữ ký trên winform Signature Pad
[C#] Hướng dẫn viết sự kiện Delay Text Change cho Text Box trong csharp
Bài viết hôm nay, mình sẽ hướng dẫn các bạn bắt sự kiện delay text change trong Textbox của C#.
C# Event Delay Text Change in Text Box
Nếu bạn nào thường viết tìm kiếm cho dữ liệu, thì thường là bắt sự kiện TextChange của Text Box.
Ví dụ: Dưới đây là câu truy vấn tìm kiếm mà các bạn thường hay sử dụng.
Select * from tbl_sinhvien where tensv like '%query%'
Nếu các bạn bắt sự kiện cho textChange, nếu bạn nhập chữ: Nguyen van a (bao gồm 12 ký tự). Vậy là mỗi lần text box bắt sự kiện text change thì nó sẽ truy xuất dữ liệu Sqlserver của mình 12 lần (không tối ưu). Nếu bạn tìm kiếm trên bảng table mà cơ sở dữ liệu lớn sẽ xảy ra tình trạng bị lag.
Cách xử lý: Sau khi các bạn gõ xong text thì đợi khoảng 2 giây, mới thực hiện lệnh của text change.
Dưới đây là demo ứng dụng C#:
Source code C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TextChange_Delay
{
public partial class Form1 : Form
{
TypeAssistant assistant;
public Form1()
{
InitializeComponent();
}
void assistant_Idled(object sender, EventArgs e)
{
this.Invoke(
new MethodInvoker(() =>
{
lblResult.Text = txtInput.Text;
}));
}
private void txtInput_TextChanged(object sender, EventArgs e)
{
int delay = int.Parse(txtDelay.Text);
assistant = new TypeAssistant(delay);
assistant.Idled += assistant_Idled;
assistant.TextChanged();
}
}
public class TypeAssistant
{
public event EventHandler Idled = delegate { };
public int WaitingMilliSeconds { get; set; }
System.Threading.Timer waitingTimer;
public TypeAssistant(int waitingMilliSeconds = 300)
{
WaitingMilliSeconds = waitingMilliSeconds;
waitingTimer = new System.Threading.Timer(p =>
{
Idled(this, EventArgs.Empty);
});
}
public void TextChanged()
{
waitingTimer.Change(WaitingMilliSeconds, System.Threading.Timeout.Infinite);
}
}
}
HAVE FUN :)