- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [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
[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 :)