using System; using System.Windows.Forms; using System.Timers; namespace WindowsFormsApplication1 { public class AutoCommitTextBox : TextBox { public const int MIN_COMMIT_COUNT = 2; public const int AUTO_COMMIT_INTERVAL = 500; public delegate void CommitEventHandle(); public event CommitEventHandle CommitEvent; private System.Timers.Timer timer = new System.Timers.Timer(); private bool isCommit; public AutoCommitTextBox() : base() { timer.Interval = AUTO_COMMIT_INTERVAL; timer.Enabled = false; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); // Auto commit for empty string as well if (this.Text.Length > 0 && this.Text.Length <= MIN_COMMIT_COUNT) return; timer.Enabled = false; timer.Enabled = true; isCommit = false; } private void timer_Elapsed(object sender, ElapsedEventArgs e) { if (!isCommit) { timer.Enabled = false; isCommit = true; CommitText(); } } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.KeyCode == Keys.Enter) { timer.Enabled = false; CommitText(); } } private void CommitText() { MethodInvoker mi = new MethodInvoker(Commit); this.Invoke(mi); } private void Commit() { if (CommitEvent != null) CommitEvent(); } } } How to use it: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.autoCommitTextBox1.CommitEvent += new AutoCommitTextBox.CommitEventHandle(autoCommitTextBox1_CommitEvent); } private void autoCommitTextBox1_CommitEvent() { this.textBox1.Text = this.autoCommitTextBox1.Text; } }